javascript Jquery 自动完成选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13784745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:46:44  来源:igfitidea点击:

Jquery Autocomplete selected value

javascriptjquery

提问by Bill

I have a autocomplete on the page using jQuery UI and here is the JSON:

我使用 jQuery UI 在页面上有一个自动完成功能,这里是 JSON:

[ {"label":"test1", "value":"1"},
  {"label":"testtest", "value":"6"},
  {"label":"test2", "value":"8"} ]

The default action of autocomplete will grab the value of the item and place into the input box. Is there a way I can stop it? Instead have two separate actions: one inject to a hidden box (with value) and the other to the input box with (label).

自动完成的默认操作将获取项目的值并放入输入框中。有什么办法可以阻止它吗?而是有两个单独的操作:一个注入隐藏框(带有值),另一个注入带有(标签)的输入框。

$.getJSON('index.php?controller=account&action=getusers', function(data) {
    tempJson = data;

    $(".auto-search").autocomplete({
        minLength: 2,
        dataType: 'json',
        source: tempJson,
        select: function (event,ui) {
            $('input[name="user-id"]').val(ui.item.value);
        }
    });
});

回答by Sudhir Bastakoti

Do you mean something like:

你的意思是这样的:

........
select: function (event,ui){
    $('input[name="user-id"]').val(ui.item.label);
    $('input[name="your-hidden-field"]').val(ui.item.value);
    return false;
}

回答by SPlatten

This is the actual HTML created by the combobox script:

这是组合框脚本创建的实际 HTML:

    <p class="dlgline">Select activity:<br>
        <select id="biActivity_id" style="display: none;"><option value="1">2015/12/23 14:26 for 1 hour, 3 minutes, Demo</option></select>
        <span class="custom-combobox"><input title="" class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" autocomplete="off"><a tabindex="-1" title="Show All Items" class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" role="button"><span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span><span class="ui-button-text"></span></a></span>
    </p>
    <p class="dlgline">Select project:<br>
        <select id="biProj_id" style="display: none;"><option value="1">Productive Non Contract</option><option value="2">Non Project Sales</option><option value="3">Waiting Time</option><option value="5">BEL SEM gateway</option><option value="6">Electronic Timesheets</option><option value="7">Fieldbus Speed Module</option><option value="8">Power Management Systems Design</option></select><span class="custom-combobox"><input title="" class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" autocomplete="off"><a tabindex="-1" title="Show All Items" class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" role="button"><span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span><span class="ui-button-text"></span></a></span>
    </p>