JQuery autocomplete

試著忘記壹切 提交于 2019-12-08 06:38:11

问题


In Jquery when i select the matched record, i want to allow only numbers in the selected record to be placed in the textbox. i want to know how to do this in autocomplete. for example

when i type 75 in the textbox it gives me the results like below

7510- Synaptic
7512-Allied
7513-King

when i select the particular row , the number in the seleted value should only be placed in the textbox.

 -- >  7513-King

7513 should be placed in textbox.


回答1:


Are you using the jQuery UI autocomplete ? If so, pass in an initial array of Json objects with both label and value properties. The label text will be displayed in the autocomplete dropdown, but the value is available on the select event.

var data = [
            { label: "7510-Synaptic", value: 7510 },
            { label: "7512-Allied", value: 7512 },
            { label: "7513-King", value: 7513 }
        ];

$( "#theAutocomplete" ).autocomplete({
    delay: 0,
    source: data, 
    select: function(event, ui) { 
        // selected value is at ui.item.value; 
        // label is at ui.item.label
     }
}).



回答2:


This should do it, although a regex expression would be also usefull and maybe more accurate.

parseInt('7513-King');



回答3:


This can be done with the original standalone jQuery autocompleter plugin that is still around (and actively developed):

http://code.google.com/p/jquery-autocomplete/

You will have to use the option

onItemSelect

There is an example that does just this in the demo (http://code.google.com/p/jquery-autocomplete/source/browse/demo/index.html)



来源:https://stackoverflow.com/questions/4920031/jquery-autocomplete

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