Jquery 如何通过单击单选按钮显示/隐藏DIV?
默认情况下,两个DIV都应隐藏。通过选择单选按钮,必须显示相应的DIV(在其#ID之后) 下面是我的代码:Jquery 如何通过单击单选按钮显示/隐藏DIV?,jquery,radio-button,Jquery,Radio Button,默认情况下,两个DIV都应隐藏。通过选择单选按钮,必须显示相应的DIV(在其#ID之后) 下面是我的代码: <input type="radio" name='thing' value='valuable' id="bank"/> <input type="radio" name='thing' value='valuable' id="school"/> <div id="bank" class="none">Bank</div> <di
<input type="radio" name='thing' value='valuable' id="bank"/>
<input type="radio" name='thing' value='valuable' id="school"/>
<div id="bank" class="none">Bank</div>
<div id="school" class="none">School</div>
<style> .none { display:none; } </style>
银行
学校
.none{display:none;}
但我们一次只能播放一个div。有可能吗?试试这个:
HTML:
您可以在此处使用
data-*
属性,如:
HTML
首先,对于2个或更多控件使用相同的id并不是更好的方法 请为每个元素创建一个唯一的id 使用此代码
<input type="radio" name='thing' value='valuable' id="bank"/>
<input type="radio" name='thing' value='valuable' id="school"/>
<div id="radio_divs">
<div id="bank_div" class="none">Bank</div>
<div id="school_div" class="none">School</div>
</div>
<style> .none { display:none; }
.view { display:block; }
</style>
它肯定会起作用,使它只显示一个div,而rest div使它不可见……这是我使用jQuery的解决方案:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function showBank()
{
$("#bankDIV").removeClass("none");
$("#bankDIV").addClass("showDIV");
//Make sure schoolDIV is not visible
$("#schoolDIV").removeClass("showDIV");
$("#schoolDIV").addClass("none");
}
function showSchool()
{
$("#schoolDIV").removeClass("none");
$("#schoolDIV").addClass("showDIV");
//Make sure bankDIV is not visible
$("#bankDIV").removeClass("showDIV");
$("#bankDIV").addClass("none");
}
</script>
</head>
<body>
<input type="radio" name='thing' value='valuable' id="bank" onclick="showBank()"/>
<input type="radio" name='thing' value='valuable' id="school" onclick="showSchool()"/>
<div id="bankDIV" class="none">Bank</div>
<div id="schoolDIV" class="none">School</div>
<style>
.none { display:none; },
.showDIV { display:block; }
</style>
</body>
</html>
函数showBank()
{
$(“#bankDIV”).removeClass(“无”);
$(“#bankDIV”).addClass(“showDIV”);
//确保schoolDIV不可见
$(“#schoolDIV”).removeClass(“showDIV”);
$(#schoolDIV”).addClass(“无”);
}
函数showSchool()
{
$(“#schoolDIV”).removeClass(“无”);
$(“#schoolDIV”).addClass(“showDIV”);
//确保bankDIV不可见
$(“#bankDIV”).removeClass(“showDIV”);
$(“#bankDIV”).addClass(“无”);
}
银行
学校
.none{display:none;},
.showDIV{display:block;}
确保代码中没有出现双重ID。一次只能看到一个DIV
问候,,
Alex你也可以试试这个
<input type="radio" name='thing' value='valuable' id="bank"/> <input type="radio" name='thing' value='valuable' id="school"/>
<div id="bankDiv" class="none">Bank</div> <div id="schoolDiv" class="none">School</div>
jquery
$(function() {
$('input[type="radio"]').change(function() {
var rad = $(this).attr('id');
$('#' + rad + 'Div').show();
$('#' + rad + 'Div').siblings('div').hide();
});
});
查看此小提琴:这可以使用CSS完成 注意:
必须是
元素的一般同级
HTML:
jsFIDLE:这对我来说很有用:
<div class="form-group">
<label>Please select Your Payment Option:</label><br>
<input id='watch-me1' name='payment' type='radio' class='show-me1' value="Cash On Delivery" checked /> Cash On Delivery<br />
<div id='show-me1' style='display:none;border:1px solid black;color:red;'>Not Available At This Moment!</div>
<input id='watch-me2' name='payment' type='radio' class='show-me2' value="Bkash/Rocket Mobile" /> Bank<br />
<div id='show-me2' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<input id='watch-me3' name='payment' type='radio' class='show-me3' value="Credit/Debit Card" /> Credit/Debit Card<br />
<div id='show-me3' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<input id='watch-me4' name='payment' type='radio' class='show-me4' value="Paypal" /> Paypal<br />
<div id='show-me4' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<script>
$(function(){
$(':radio').click(function() {
$('#' + $(this).attr('class')).fadeIn().siblings('div').hide();
})
.filter(':checked').click();
});
</script>
</div>
请选择您的付款选项:
货到付款
目前不可用!
银行
目前不可用!
信用卡/借记卡
目前不可用!
贝宝
目前不可用!
$(函数(){
$(':radio')。单击(函数(){
$('#'+$(this.attr('class')).fadeIn().sides('div').hide();
})
.filter(“:选中”)。单击();
});
jquery for onchange event的call hide()函数在一个页面上不要使用两个相同的id。正如@KamilT所说,您的HTML在此处无效。这不会在单击时显示相应的div,但会隐藏它有关此代码的一些附加说明。唯一的答案可能是有益的。
$(':radio').change(function (event) {
var id = $(this).data('id');
$('#' + id).addClass('none').siblings().removeClass('none');
});
<input type="radio" name='thing' value='valuable' id="bank"/>
<input type="radio" name='thing' value='valuable' id="school"/>
<div id="radio_divs">
<div id="bank_div" class="none">Bank</div>
<div id="school_div" class="none">School</div>
</div>
<style> .none { display:none; }
.view { display:block; }
</style>
jQuery("input[type=radio]").click(function(event) {
jQuery('#radio_divs').children('div').each(function () {
jQuery(this).addClass(".none");
});
jQuery("#" + this.id + "_div").addClass(".view");
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function showBank()
{
$("#bankDIV").removeClass("none");
$("#bankDIV").addClass("showDIV");
//Make sure schoolDIV is not visible
$("#schoolDIV").removeClass("showDIV");
$("#schoolDIV").addClass("none");
}
function showSchool()
{
$("#schoolDIV").removeClass("none");
$("#schoolDIV").addClass("showDIV");
//Make sure bankDIV is not visible
$("#bankDIV").removeClass("showDIV");
$("#bankDIV").addClass("none");
}
</script>
</head>
<body>
<input type="radio" name='thing' value='valuable' id="bank" onclick="showBank()"/>
<input type="radio" name='thing' value='valuable' id="school" onclick="showSchool()"/>
<div id="bankDIV" class="none">Bank</div>
<div id="schoolDIV" class="none">School</div>
<style>
.none { display:none; },
.showDIV { display:block; }
</style>
</body>
</html>
<input type="radio" name='thing' value='valuable' id="bank"/> <input type="radio" name='thing' value='valuable' id="school"/>
<div id="bankDiv" class="none">Bank</div> <div id="schoolDiv" class="none">School</div>
.none { display:none; }
$(function() {
$('input[type="radio"]').change(function() {
var rad = $(this).attr('id');
$('#' + rad + 'Div').show();
$('#' + rad + 'Div').siblings('div').hide();
});
});
<form>
<input type="radio" name="showGreen"> Green
<input type="radio" name="showRed"> Red
<div id="green">I am the green div.</div>
<div id="red">I am the red div.</div>
</form>
#green, #red {
display: none;
padding: 10px;
}
#green {
background-color: #6f6;
}
#red {
background-color: #f44;
}
input[name="showGreen"]:checked ~ #green,
input[name="showRed"]:checked ~ #red {
display: block;
}
<div class="form-group">
<label>Please select Your Payment Option:</label><br>
<input id='watch-me1' name='payment' type='radio' class='show-me1' value="Cash On Delivery" checked /> Cash On Delivery<br />
<div id='show-me1' style='display:none;border:1px solid black;color:red;'>Not Available At This Moment!</div>
<input id='watch-me2' name='payment' type='radio' class='show-me2' value="Bkash/Rocket Mobile" /> Bank<br />
<div id='show-me2' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<input id='watch-me3' name='payment' type='radio' class='show-me3' value="Credit/Debit Card" /> Credit/Debit Card<br />
<div id='show-me3' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<input id='watch-me4' name='payment' type='radio' class='show-me4' value="Paypal" /> Paypal<br />
<div id='show-me4' style='display:none;border:1px solid black;'>Not Available At This Moment!</div>
<script>
$(function(){
$(':radio').click(function() {
$('#' + $(this).attr('class')).fadeIn().siblings('div').hide();
})
.filter(':checked').click();
});
</script>
</div>