javascript - jquery autocomplete How to set label and how to get value? - Stack Overflow

I have a dynamic input field created with class name as sub-category.On key press autoplete is working

I have a dynamic input field created with class name as sub-category. On key press autoplete is working fine, from which i can select one.

Working code for that is below

$("body").on('keypress', 'input.sub-category', function () {
        var availableTags = [
            {label:"ActionScript", value:"1"},
            {label:"ActionScript1", value:"11"},
            {label:"ActionScript2", value:"12"},
            {label:"ActionScript3", value:"13"},
            {label:"ActionScript4", value:"14"},
        ];
        $(this).autoplete({
            source: availableTags,
        });
    });

When i select one of the autosuggested text say 'ActionScript', it's value (1) should be available in the below code, how i can access it? On select alert function is working fine, so just need to know how to access value of the selected label.

$("body").on('autopleteselect', 'input.sub-category', function () {
        alert('here');
});

In addition to that i would like to know how to set the selected text in the input box rather than it's value.

I have a dynamic input field created with class name as sub-category. On key press autoplete is working fine, from which i can select one.

Working code for that is below

$("body").on('keypress', 'input.sub-category', function () {
        var availableTags = [
            {label:"ActionScript", value:"1"},
            {label:"ActionScript1", value:"11"},
            {label:"ActionScript2", value:"12"},
            {label:"ActionScript3", value:"13"},
            {label:"ActionScript4", value:"14"},
        ];
        $(this).autoplete({
            source: availableTags,
        });
    });

When i select one of the autosuggested text say 'ActionScript', it's value (1) should be available in the below code, how i can access it? On select alert function is working fine, so just need to know how to access value of the selected label.

$("body").on('autopleteselect', 'input.sub-category', function () {
        alert('here');
});

In addition to that i would like to know how to set the selected text in the input box rather than it's value.

Share Improve this question asked Jul 18, 2016 at 11:12 user866933user866933 3351 gold badge6 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

you need to include jquery-ui file "https://code.jquery./ui/1.12.0/jquery-ui.js" in your file and just replace

 $("body").on('autopleteselect', 'input.sub-category', function () {
        alert('here');
});

with the following lines.

$("body").on('autopleteselect', 'input.sub-category', function (event,ui)     {
    alert(ui.item.label);
    alert(ui.item.value);

});

and

$(this).autoplete({
        source: availableTags,
    });

with

$(this).autoplete({
        source: availableTags,
        select:function(event,ui){
           $(".sub-category").val(ui.item.label);return false;
        }
    });

and this will work fine for more info please go on following link

http://api.jqueryui./autoplete/

More than easy!

It's here http://api.jqueryui./autoplete/#event-select
And it says, that You may bind handler in such manner

$( ".selector" ).on( "autopleteselect", function( event, ui ) {} );

where ui is Object which has item which in turn has selected label and value data.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744966938a4603737.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信