Comparing two jquery ui autocomplete combobox values?

限于喜欢 提交于 2019-12-08 10:02:54

问题


How to compare two jquery ui autocomplete combobox values, not input values that are generated by jquery but <select><option value="foo"></option></select> values? How to get this values and compare them, everytime they are changed, either one of them?


回答1:


I did not reallt understand your question correctly the first time.

I suppose you took the combobox code from the jQuery UI Autocomplete demo page ?

If you look at the code, it triggers an event "selected" when a selection happens:

select: function(event, ui) {
    ui.item.option.selected = true;
    // triggers the "selected" event
    self._trigger("selected", event, {
        item: ui.item.option
    });
},

As the combobox example uses the jQuery UI Widget Factory, you can easily bind an event handler for this event from the options when initializing the plugin instance:

$( "#combobox" ).combobox({ 
    selected: function(e, ui) {
        // in here "ui.item" is the <option> selected
        // so you can use "ui.item.value"
    }
});

This will act as sort-of "change handler". In it, you have access to the value through ui.item.value.

Working example on jsfiddle.




回答2:


Try storing each value in an element, using jquerys data method: http://api.jquery.com/data/

Usage example:

$('#combobox10').data('key', 'value');

edit:

Set the value:

$('#combobox10').data('key', 'foo');

Get the value:

var combobox10value = $('#combobox10').data('key');
alert(combobox10value); // will alert 'foo'


来源:https://stackoverflow.com/questions/8834316/comparing-two-jquery-ui-autocomplete-combobox-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!