更改事件上的 jQuery UI 自动完成功能未实时触发

问题描述 投票:0回答:2

使用 jQuery UI,我尝试在

#tag
输入的值发生更改时立即触发事件,但如您所见,如果用户单击页面,而不是在输入变化
我已经尝试将更改事件与 

log

绑定,但我得到了相同的结果。

paste
 $( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
    
     $("#tags").on('change', function(){
        console.log($(this).val());
    });
  } );

我该如何解决这个问题?

javascript jquery jquery-ui jquery-ui-autocomplete
2个回答
1
投票
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div>

事件仅在输入值更改后才会触发。变化并不一定意味着价值立即变化。更改意味着输入值已提交为不同的值。如果您希望每次执行操作时都发生一些事情,请使用输入事件。


Change
$( function() {
        var availableTags = [
          "ActionScript",
          "AppleScript",
          "Asp",
          "BASIC",
          "C",
          "C++",
          "Clojure",
          "COBOL",
          "ColdFusion",
          "Erlang",
          "Fortran",
          "Groovy",
          "Haskell",
          "Java",
          "JavaScript",
          "Lisp",
          "Perl",
          "PHP",
          "Python",
          "Ruby",
          "Scala",
          "Scheme"
        ];
        $( "#tags" ).autocomplete({
          source: availableTags
        });
        
         $("#tags").on('input', function(){
            console.log($(this).val());
        });
      } );


0
投票

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <label>Input:</label> <input type='text' value='' id='tags'/>

keypress
 代替 
keydown
,只要用户在输入中写入,代码就会触发。

change

© www.soinside.com 2019 - 2024. All rights reserved.