Jquery自动填充选定的值

时间:2012-12-09 04:47:25

标签: javascript jquery

我在页面上使用jQuery UI进行自动完成,这里是JSON:

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

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

$.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);
        }
    });
});

2 个答案:

答案 0 :(得分:8)

你的意思是:

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

答案 1 :(得分:0)

这是组合框脚本创建的实际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>