动态创建这些字段时,如何检查单选按钮是否被选中,然后为其分配值。
这是我尝试过的JQuery:
$('#want').change(function() {
$(this).closest('.from-group').
val(($('#want').is(':checked')) ? "yes" : "no");
var ans=$(this).val();
alert(ans);
});
但是它没有为动态字段设置值,如example所示。
最佳答案
您必须像这样委派您的活动:DEMO
$(document).on('change','#want',function() {
$(this).closest('.from-group').
val(($('#want').is(':checked')) ? "yes" : "no");
var ans=$(this).val();
alert(ans);
});
还请记住,ID必须是唯一的,您的代码会生成重复的ID