gpt4 book ai didi

javascript - 通过使用 jquery 检查选​​中哪个单选按钮来执行事件

转载 作者:可可西里 更新时间:2023-11-01 13:24:15 25 4
gpt4 key购买 nike

我需要根据选中的单选按钮显示不同的 div。我已经通过使用更改事件方法完成了它。但是我需要在加载页面时通过查看选中的按钮来完成它。

$('#id_radio1').click(function () {
$('#div2').hide('fast');
$('#div1').show('fast');
});

单选按钮是

<div class="col-md-4">       
<label>
<input type="radio" name="sublink" id="id_radio2" value="0" <?php if($announcements_details['detail_type']=="0") echo' checked="checked"'?> >&nbsp;Add attachment to title</input>
</label>
</div>
<div class="col-md-4">
<label>
<input type="radio" name="sublink" id="id_radio1" value="1" <?php if($announcements_details['detail_type']=="1") echo' checked="checked"'?> >&nbsp;Add new sublinks</input>
</label>
</div>
<div class="col-md-4">
<label>
<input type="radio" name="sublink" id="id_radio3" value="2" <?php if($announcements_details['detail_type']=="2") echo' checked="checked"'?>>&nbsp;None
</label>
</div>

帮我解决这个问题

最佳答案

JS 查询

在页面加载时检查 radio 。

$("input[name=sublink]").each(function(index, elem){
if($(this).prop("checked")){

console.log("radio (with checked) has value: "+elem.value);

if(elem.value == "0"){
// LOGIC FOR SHOW/HIDE DIV HERE
} else if(elem.value == "1"){
// LOGIC FOR SHOW/HIDE DIV HERE
} else if(elem.value == "2"){
// LOGIC FOR SHOW/HIDE DIV HERE
}
}
});

.....

如果你需要这个。单击输入 radio 时检查 radio 。

$("input[name=sublink]").on("change", function(){
switch(this.value){
case "0":
console.log("radio with value 0");
// LOGIC FOR SHOW/HIDE DIV HERE
break;
case "1":
console.log("radio with value 1");
// LOGIC FOR SHOW/HIDE DIV HERE
break;
case "2":
console.log("radio with value 2");
// LOGIC FOR SHOW/HIDE DIV HERE
break;
default:
console.log("default");
}
});

关于javascript - 通过使用 jquery 检查选​​中哪个单选按钮来执行事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41563704/

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