jQuery 选择的插件更改事件未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21277033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:30:57  来源:igfitidea点击:

Chosen plugin change event not triggering

jqueryjquery-pluginsonchangejquery-chosenjquery-events

提问by user3093453

I'm using ChosenjQuery plugin and noticed that the changeevent is working onlywhen the page loads, NOTevery time the inputfield is being change.

我正在使用ChosenjQuery 插件并注意到该change事件在页面加载时才起作用,而不是在每次input更改字段时起作用。

How can I make it work every time the user changes the value of the input field?

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

Here is my code:

这是我的代码:

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

Am I missing something?

我错过了什么吗?

回答by Soundar

To fire the standard change event, use like:

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

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

To fire the event on each key press,

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

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

To know about the default events of chosen, refer here.

要了解 selected 的默认事件,请参阅此处

回答by ace313

Try this:

尝试这个:

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

回答by AkshayKumar

Updating Chosen Dynamically

动态更新选择

If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

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

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

回答by Milind Anantwar

Then Write it in keyup event of input:

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

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

and on dom ready:

并在 dom 上准备好:

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

回答by Muddassir Irahad

    //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());
        }