【问题标题】:Chosen plugin change event not triggering选择的插件更改事件未触发
【发布时间】:2014-02-12 03:54:36
【问题描述】:

我正在使用Chosen jQuery 插件并注意到change 事件在页面加载时起作用,不是每次input 字段正在改变。

如何在每次用户更改输入字段的值时使其工作?

这是我的代码:

 $("#day").chosen().change({
     console.log('working');
  });

我错过了什么吗?

【问题讨论】:

标签: jquery jquery-plugins onchange jquery-chosen jquery-events


【解决方案1】:

要触发标准更改事件,请使用如下:

  $('#day').on('change', function(e) {
    // triggers when whole value changed
    console.log("value changed");
  });

要在每次按键时触发事件,

  $('#day').on('keyup', function(e) {
    // triggers when each key pressed
    console.log("key pressed");
  });

要了解选择的默认事件,请参考here

【讨论】:

    【解决方案2】:

    试试这个:

    $(document).ready(function(){
        $("selector").chosen().change(function(){
            var id = $(this).val();
        });
    })
    

    【讨论】:

    • 它对我有用,我可以控制价值.. $(document).ready(function(){ $("selector").chosen().change(function (event) { console.log($(event.target).val()); }); })
    【解决方案3】:

    动态更新选择

    如果您需要更新选择字段中的选项并希望 Chosen 获取更改,则需要在该字段上触发“chosen:updated”事件。 Chosen 将根据更新的内容重新构建自己。

    $("#foo").trigger("chosen:updated");

    【讨论】:

    • 这与OP问题无关
    【解决方案4】:

    多选

    当设置了选择的倍数时,$(this).val(); 返回一个array。 以下代码将返回最后一个 id [un]selected:

    $('#my_chosen_list').chosen().change(function(_, object){
        console.log(!object.selected ? object.selected : object.deselected);
        ...
    });
    

    提示:undefined 未定义 (...),但 !undefined 为真,因此 !!undefined 为假

    【讨论】:

      【解决方案5】:

      然后写在输入的keyup事件中:

      $('inputselector').keyup(function() {
        $("#day").chosen().change({
         console.log('working');
        });
      });​
      

      并且在 dom 准备就绪:

      $('inputselector').trigger('keyup');
      

      【讨论】:

        【解决方案6】:
            //Asp.net dropdown listbox
          <asp:ListBox ID="CompanySelect" runat="server" AppendDataBoundItems="true" 
          AutoPostBack="true"   
          data-placeholder="Select Companies" class="chosen-select"  SelectionMode="Multiple" 
          EnableViewState="true" Height="39px" Width="99%" >
          <asp:ListItem Value="0">All</asp:ListItem>
          </asp:ListBox>
        
            //This code help me for hiting chozen change event.
        
             $('#chosen').val('').change()
                {
                   alert("working");
                }
        
            //if you want to get select value you need to do this.
        
             $('#chosen').val('').change()
                {
                    alert($(".chosen-select").val());
                }
        

        【讨论】:

        • 虽然此代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期价值。请edit您的答案添加解释,包括适用的限制和假设。
        猜你喜欢
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多