gpt4 book ai didi

javascript - 在动态创建的字段上设置单选按钮值

转载 作者:行者123 更新时间:2023-12-02 16:56:11 24 4
gpt4 key购买 nike

如何检查单选按钮是否被选中,然后在动态创建这些字段时为其分配值。

这是我尝试过的 JQuery:

$('#want').change(function() {
$(this).closest('.from-group').
val(($('#want').is(':checked')) ? "yes" : "no");
var ans=$(this).val();
alert(ans);
});

但它没有为动态字段设置值,如example所示.

最佳答案

您必须像这样委托(delegate)您的事件: DEMO

$(document).on('change','#want',function() {
$(this).closest('.from-group').
val(($('#want').is(':checked')) ? "yes" : "no");
var ans=$(this).val();
alert(ans);
});

还要记住id 必须是唯一的,您的代码会生成重复的 id

关于javascript - 在动态创建的字段上设置单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com