gpt4 book ai didi

jquery - 根据另一个 radio 设置 radio

转载 作者:行者123 更新时间:2023-12-01 07:07:39 24 4
gpt4 key购买 nike

我有这个表格:

<form name="test">
<input type="radio" required="required" name="a" value="1">1
<input type="radio" required="required" name="a" value="X">X
<input type="radio" required="required" name="a" value="2">2
<br />
<input type="radio" required="required" name="b" value="1">1
<input type="radio" required="required" name="b" value="2">2
</form>

我想实现这个:

If a=1 then b=1
If a=X then unset b
If a=2 then b=2

我尝试了这个,但没有任何作用:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#test').on('change', function() {
if ($("input[name='a']:checked").val()=="1") {
$( "#b" ).prop( "checked", true );
}
});
});
</script>

这里缺少什么?

最佳答案

在名为aradio按钮上绑定(bind)更改事件,获取其值,在名为b的radio按钮中找到相同的值设置,并查看其属性,如下:

$('input[name="a"]').on('change', function() {
var selected = $('input[name="a"]:checked').val();
if (selected == "X") {
$('input[name="b"]:checked').prop("checked", false);
return false;
}
$('input[name="b"][value="' + selected + '"]').prop("checked", true);

});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<form name="test">
<input type="radio" required="required" name="a" value="1">1
<input type="radio" required="required" name="a" value="X">X
<input type="radio" required="required" name="a" value="2">2
<br />
<input type="radio" required="required" name="b" value="1">1
<input type="radio" required="required" name="b" value="2">2
</form>

PS: If you wish you can add disabled property to radio button set with name b so that user is not allowed to change them

关于jquery - 根据另一个 radio 设置 radio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37003751/

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