【问题标题】:Select first search item without clicking to it in Jquery选择第一个搜索项而不在 Jquery 中单击它
【发布时间】:2016-09-08 18:52:29
【问题描述】:

您好,我有一个 html 文件,其中包含多个输入,当在自动完成时选择匹配项时会自动填充这些输入

我的html文件是:

<table class="table table-bordered">
    <ul>
        <li><label>Barcode</label>
            <input class="form-control" type='text' id='barcodescanr' name='barcodescanr' />
        </li>
        <li><label>Surname  </label>
            <input class="form-control" type='text' id='surname_1' name='surname' required/>
        </li>
        <li>
            <label>Name</label> 
            <input class="form-control" type='text' id='name_1' name='name' required/>
        </li>
        <li><label>Company Name</label>
            <input class="form-control" type='text' id='company_name_1' name='company_name' required/>
        </li>
    </ul>
</table>

我的搜索看起来像这样

填充输入字段的自动完成功能是这样的,它通过条形码扫描工作

$('#barcodescanr').autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'ajax.php',
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
               type: 'barcodescanr',
               row_num : 1
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    var code = item.split("|");
                    return {
                        label: code[0],
                        value: code[0],
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,
    minLength: 3,
    select: function( event, ui ) {
        var names = ui.item.data.split("|");                    
        $('#name_1').val(names[2]);
        $('#company_name_1').val(names[3]);
        $('#surname_1').val(names[1]);
        $('#firm_1').val(names[4]);
        $('#info').val(names[5]);
        $('#scanbartime').val(names[6]);
        $('#id').val(names[7]);
        $('#custId').val(names[8]);
    },
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }               
 });                        

如何在不点击自动完成的情况下选择第一个匹配的条形码?谢谢

【问题讨论】:

    标签: javascript jquery html json ajax


    【解决方案1】:

    试试这个

    $('#barcodescanr').change(function(){
         $('#ui-id-1 li:first-child').trigger('click');
     });
    

    或者这个,当用户停止写作时

    //setup before functions
    var typingTimer;                //timer identifier
    var doneTypingInterval = 2000;  //time in ms, 2 second for example
    var $input = $('#barcodescanr');
    
    //on keyup, start the countdown
    $input.on('keyup', function () {
      clearTimeout(typingTimer);
      typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });
    
    //on keydown, clear the countdown
    $input.on('keydown', function () {
      clearTimeout(typingTimer);
    });
    
    //user is "finished typing," do something
    function doneTyping () {
      $('#ui-id-1 li:first-child').trigger('click');
    }
    

    根据自己的喜好使用doneTypingInterval 的值。

    【讨论】:

    • 是的,非常感谢。您还知道在选择项目进行下一次搜索后如何清除#barcodescanr 吗?
    • $('#barcodescanr').val(''); 添加在函数 doneTyping() 的末尾
    【解决方案2】:

    $('.table-bordered ul li:first-child').click();

    【讨论】:

    • 不,它不起作用。我实际上是在 $(#barcodesurname).change(function(event)); 下运行它的;
    猜你喜欢
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2023-02-23
    相关资源
    最近更新 更多