开发者

jQuery UI Autocomplete value after mouse click is old value

开发者 https://www.devze.com 2023-03-12 01:18 出处:网络
I\'m using the jQuery UI Autocomplete widget. I need to use the selected text for further processing, direct after change or select.

I'm using the jQuery UI Autocomplete widget. I need to use the selected text for further processing, direct after change or select.

But when a user clicks the suggested value, the typed value gets passed.

$( "#autocomplete" ).autocomplete({
source: function( req, resp ) {
    $.post( "/echo/json/", {
        json: '["Test1", "Test2", "Test3", "Test4", "Test5"]',
        delay: 1
    }, function(data) {
        resp( data );
    }, "JSON" );
},
select: function (event, ui) { alert($(this).val())}
});

An example at this fiddle: (try typing "tes" and select a suggestion by mouse)

http://jsfiddle.net/M3Yur/

i need to use the input value. a change events uses the input value, it needs to work in both cases when a autocomplete item is selected, but also 开发者_Python百科when just a value is typed so the alert is just for the example.

Any way to get around this?

EDIT: i ended up using: $(this).val(ui.item.value).val(); this solutions always gets the intended value after a change event.


Change this:

select: function (event, ui) { alert($(this).val())}

To this:

select: function (event, ui) {alert(ui.item.value);}

New fiddle: http://jsfiddle.net/M3Yur/4/


The ui argument contains the information you're looking for:

select: function (event, ui) { alert(ui.item.value); }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号