我需要根据选中的单选按钮显示不同的div。我已经通过使用change事件方法来做到这一点,但是当我通过查看选中的页面来加载页面时需要这样做。
$('#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"'?> > 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"'?> > 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"'?>> None
</label>
</div>
帮我解决这个问题
JS jQuery
页面加载时获取选中的单选。
$("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
}
}
});
.....
如果您需要这个。单击输入广播时,获取选中的广播。
$("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");
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句