jquery选择更新隐藏输入添加新选项

时间:2018-01-23 11:44:47

标签: jquery options jquery-chosen

我正在使用jQuery Chosen插件,并且能够动态添加新选项

$('#element').chosen({
    create_option: true,
    // persistent_create_option decides if you can add any term, even if part
    // of the term is also found, or only unique, not overlapping terms
    persistent_create_option: true,
    // with the skip_no_results option you can disable the 'No results match..' 
    // message, which is somewhat redundant when option adding is enabled
    skip_no_results: true,
    create_option_text: 'Add option'
});

我需要的是仅在单击添加选项链接时更新隐藏字段的方法。

在检查Chrome中的代码后,我可以看到添加了以下html:

<li class="create-option active-result"><a>Add option</a>: "dsgdsgsdgdg"</li>

我尝试使用点击事件定位这些类,但它不起作用

$('.create-option.active-result').click(function() {
    alert('fsgsdgdsgd');
});

有没有办法定位此链接或检查选择列表的长度是否已更改?

1 个答案:

答案 0 :(得分:0)

您希望将事件绑定到动态添加的元素。

请参阅此问题:Event binding on dynamically created elements?

作为州,你必须打电话:

$("#myList").on("click", ".create-option.active-result", function() {
    alert(this.text);
});
相关问题