gpt4 book ai didi

javascript - 在 jquery 中自动选择通信 radio

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:42 26 4
gpt4 key购买 nike

我在一个 div 中有动态 radio ,就像这里 radio 的名称是动态的,值也是:

<input type="radio" name="first[1]" value="1" />
<input type="radio" name="first[2]" value="2" />
<input type="radio" name="first[3]" value="3" />


And the second div I have which have these radios :

<input type="radio" name="second[1]" value="1" />
<input type="radio" name="second[2]" value="2" />
<input type="radio" name="second[3]" value="3" />

My question is that if I checked (yes) the div first radio button then div second radio is selected auto. It's like both div radio working together on check fo yes and no both condition.

我试着用这个来获取第一个 div 的值,但在这里我没有获取值..

<script type="text/javascript">
$(document).ready(function(){
alert('ok');
$('input:radio').on('click',function() {
var obj=$('input[name="first"]:checked').val();
alert(obj);
});
});
</script>


Can any one please help me related this?

提供一些额外的信息,例如:

最佳答案

// Add your javascript here
$(function() {
$('input[name^="first"],input[name^="second"]').on('change', function() {

let i = $(this).index();
$('input[name^="first"]').each(function(j) {
$(this).prop('checked', i === j);
});
$('input[name^="second"]').each(function(j) {
$(this).prop('checked', i === j);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="radio" name="first[1]" value="1" />A
<input type="radio" name="first[2]" value="2" />B
<input type="radio" name="first[3]" value="3" />C
</div>


<div>
<input type="radio" name="second[1]" value="1" />AA
<input type="radio" name="second[2]" value="2" />BB
<input type="radio" name="second[3]" value="3" />CC
</div>

关于javascript - 在 jquery 中自动选择通信 radio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884019/

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