代码之家  ›  专栏  ›  技术社区  ›  Robert Mark Bram

将事件处理程序挂接到jquery autocomplete组合框

  •  2
  • Robert Mark Bram  · 技术社区  · 14 年前

    我想要一些关于如何为jquery autocomplete组合框设置事件处理程序的建议: http://jqueryui.com/demos/autocomplete/#combobox .

    jquery文档中的代码示例如下:

    // Supply a callback function to handle the select event as an init option.
    $( ".selector" ).autocomplete({
       select: function(event, ui) { ... }
    });
    // Bind to the select event by type: autocompleteselect.
    $( ".selector" ).bind( "autocompleteselect", function(event, ui) {
      ...
    });
    

    我已经试过了——按照下面的描述——但它不起作用。我从这个关闭的bug报告(dev.jqueryui.com/ticket/5891)了解到我“正在实例化一个组合框,然后尝试使用autocomplete设置选项”,但我不知道如何修复它。

    // Have: <select id="comboInput" name="comboInput"> ....
    $(document).ready(function() {
       $("#comboInput").combobox();
       $("#comboInput").autocomplete({
          select: function(event, ui) {
             alert("Value selected.");
          }
       });
    });
    

    有人能告诉我怎么做吗?感谢您的帮助!

    1 回复  |  直到 13 年前
        1
  •  3
  •   Robert Mark Bram    13 年前

    简而言之,这是为“选定”事件创建带有事件处理程序的组合框所需的(应为“选择”,但组合框只是原型):

    <script language="javascript" type="text/javascript">
       $(document).ready(function() {
          // For some SELECT with ID combobox
          $("#combobox").combobox({
             selected: function(event, ui) {
                // Event handling code goes here.
             } // selected
          }); // combo
       }); // ready
    </script>
    

    有关详细信息和讨论,请参阅我的博客文章: Event Handling with the jQuery Autocomplete Combobox .