当我在按钮点击事件上使用jQuery添加一个“多标签下拉列表”时,它不会附加多标签下拉列表,而是简单的下拉列表(我想我需要在添加之后重新加载javascript文件)。因为,当我在jquery(Document)函数(.ready(){})上附加相同的内容时,它会附加多个标记下拉列表中的javascript功能。
jQuery(document).ready(function() {
//this work perfectly in .ready() function
$("#parent").append(' <select class="standardSelect"><option>hello</option><option>hello</option> </select>');
$('#sa').attr('multiple','');
//but on button click event it just append the simple dropdown of html without any javascript functionality of tags.
$( "#mybutton" ).click(function() {
//$("#parent").append(' <select class="standardSelect"><option>hello</option<option>hello</option> </select>');
)};//click()
)};//ready()
发布于 2019-06-25 04:00:45
一旦我修复了你的语法错误,一切都很好
jQuery(document).on('change', '.standardSelect', function() {
console.log('selected ' + jQuery(this).val())
})
jQuery(document).ready(function() {
//this work perfectly in .ready() function
$("#parent").append(' <select class="standardSelect"><option>hello</option><option>hello</option> </select>');
$('#sa').attr('multiple','');
//but on button click event it just append the simple dropdown of html without any javascript functionality of tags.
$( "#mybutton" ).click(function() {
$("#parent").append(' <select class="standardSelect"><option>hello</option><option>hello</option> </select>');
});//click()
});//ready()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="parent">
</div>
<button id="mybutton">Test</button>
这是一个有效的codepen
要绑定事件处理程序以动态创建元素,即加载文档时不存在的元素,您需要将处理程序绑定到父元素:
jQuery(document).on('change', '.standardSelect', function() {
console.log('selected ' + jQuery(this).val())
})
在本例中,我使用了文档it self,它是所有内容的父对象。
https://stackoverflow.com/questions/56742673
复制相似问题