【发布时间】:2018-07-02 05:25:41
【问题描述】:
我正在使用 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');
});
有没有办法定位此链接或检查选择列表的长度是否发生了变化?
【问题讨论】:
-
元素是通过 DOM 加载还是稍后动态添加?
-
有一个输入框,如果您输入的值不在原始选择中,它会提供添加到列表中的选项,以便将选项附加到选择中
-
这就解释了问题所在。当您在动态元素上绑定事件时,您必须以不同的方式进行。
标签: jquery options jquery-chosen