我有一个带有一些单选按钮的表单。当我更改选定的单选按钮时,此脚本会显示警报
$('input[type=radio][name=shipping-h]').change(function () {
alert("worked");
});
我有另一个脚本来附加具有不同值的单选按钮
$( "#addressact" ).append('<input id=new-address type=radio name=shipping-h checked>')
如果我选择新的单选按钮,警报将不起作用。
最佳答案
您正在附加一个新元素,您应该使用 event delegation
$(document).on('change', 'input[name=shipping-h]', function() {
alert("worked");
$( "#addressact" ).append('<input id="new-address" type="radio" name="shipping-h" checked/>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="addressact">
<input type="radio" name="shipping-h"/>
</div>
关于javascript - 附加单选按钮上未触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537897/