我已经根据“150”和“3m”的选择显示某个div但是如果两个都没有被选中,我就无法隐藏div?
以下代码:
$(document).ready(function() {
$('input[value="100"], input[value="3m"]').change(function() {
if ($('input[value="100"]').is(':checked') && $('input[value="3m"]').is(':checked')) {
$('div').show();
} else {
$('div').hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>100</label>
<input class="amount" type="radio" name="amount" value="100" />
<label>150</label>
<input class="amount" type="radio" name="amount" value="150" />
<br>
<br>
<label>3</label>
<input class="month" type="radio" name="month" value="3m" />
<label>6</label>
<input class="month" type="radio" name="month" value="6m" />
<div style="display: none;">
<div class="interest">
£40
</div>
<div class="total-payable">
£140
</div>
<div class="monthly-payment">
£46.67
</div>
<div class="weekly-payment">
£10.77
</div>
</div>
答案 0 :(得分:3)
目前,您只更改了100或3米单选按钮更改时div
元素的可见性 - 您希望通过更改更新任何单选按钮更改上div
元素的可见性你的选择器:
$('input[value="100"], input[value="3m"]')
到
$('input')
$(document).ready(function() {
$('input').change(function() {
if ($('input[value="100"]').is(':checked') && $('input[value="3m"]').is(':checked')) {
$('div').show();
}
else {
$('div').hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>100</label>
<input class="amount" type="radio" name="amount" value="100" />
<label>150</label>
<input class="amount" type="radio" name="amount" value="150" />
<br><br>
<label>3</label>
<input class="month" type="radio" name="month" value="3m" />
<label>6</label>
<input class="month" type="radio" name="month" value="6m" />
<div style="display: none;">
<div class="interest">
£40
</div>
<div class="total-payable">
£140
</div>
<div class="monthly-payment">
£46.67
</div>
<div class="weekly-payment">
£10.77
</div>
</div>
答案 1 :(得分:2)
对于单选按钮,更改事件仅在选中按钮时触发,而不是在未选中时触发。
一种可能的解决方案是将change事件绑定到所有(涉及的)输入。
$(document).ready(function() {
$('input').change(function() {
if ($('input[value="100"]').is(':checked') && $('input[value="3m"]').is(':checked')) {
$('div').show();
}
else {
$('div').hide();
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>100</label>
<input class="amount" type="radio" name="amount" value="100" />
<label>150</label>
<input class="amount" type="radio" name="amount" value="150" />
<br><br>
<label>3</label>
<input class="month" type="radio" name="month" value="3m" />
<label>6</label>
<input class="month" type="radio" name="month" value="6m" />
<div style="display: none;">
<div class="interest">
£40
</div>
<div class="total-payable">
£140
</div>
<div class="monthly-payment">
£46.67
</div>
<div class="weekly-payment">
£10.77
</div>
</div>
&#13;
答案 2 :(得分:1)
我会使用filter
函数和:checked
选择器执行以下操作:
// wrap in closure
$(function() {
// store these in variables for better performance
var month = $('.month'),
amount = $('.amount'),
toggleDiv = $('#toggle-div'); // give top level div to show an id rather than toggling all divs
// use common class on radios you want to bind the event to
$('.radio').on('change', function() {
if (amount.filter(':checked').val() == 100 && month.filter(':checked').val() === "3m") {
toggleDiv.show();
} else {
toggleDiv.hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>100</label>
<input class="amount radio" type="radio" name="amount" value="100" />
<label>150</label>
<input class="amount radio" type="radio" name="amount" value="150" />
<br>
<br>
<label>3</label>
<input class="month radio" type="radio" name="month" value="3m" />
<label>6</label>
<input class="month radio" type="radio" name="month" value="6m" />
<div id="toggle-div" style="display: none;">
<div class="interest">
£40
</div>
<div class="total-payable">
£140
</div>
<div class="monthly-payment">
£46.67
</div>
<div class="weekly-payment">
£10.77
</div>
</div>
答案 3 :(得分:0)
您需要将所有单选按钮ID或名称设置为执行功能
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>100</label>
<input class="amount" type="radio" name="amount" value="100" id="100rad"/>
<label>150</label>
<input class="amount" type="radio" id="150rad"name="amount" value="150" />
<br><br>
<label>3</label>
<input class="month" type="radio" id="3mrad" name="month" value="3m" />
<label>6</label>
<input class="month" type="radio" name="month" id="6mrad" value="6m" />
<div style="display: none;">
<div class="interest">
£40
</div>
<div class="total-payable">
£140
</div>
<div class="monthly-payment">
£46.67
</div>
<div class="weekly-payment">
£10.77
</div>
</div>
JS
$(document).ready(function() {
$('#3mrad, #150rad,#100rad,#6mrad').change(function() {
if ($('#150rad').is(':checked') && $('#3mrad').is(':checked')) {
$('div').show();
}
else {
$('div').hide();
}
});
});
检查小提琴并告诉我这是你要求的
答案 4 :(得分:0)
$(document).ready(function(){
$("input[type='button']").click(function(){
var radioValue = $("input[name='gender']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
}
});
if(radioValue=='male'){
$("#divMale").show();
$("#divFemale").hide();
}
else{
$("#divMale").hide();
$("#divFemale").show();
}
});
答案 5 :(得分:0)
请改用以下javascript代码。 。 .set更改input[name="month"], input[name="amount"]
上的侦听器
$(document).ready(function() {
$('input[name="month"], input[name="amount"]').change(function() {
console.log("haha")
if ($('input[value="100"]').is(':checked') && $('input[value="3m"]').is(':checked')) {
$('div').show();
}
else {
$('div').hide();
}
});
});
答案 6 :(得分:0)
如果您使用的是 bootstrap css,您甚至可以使代码更具可移植性。
$(document).ready(function () {
$('input.rbtoggle').change(function () {
$('div.dvtoggle').toggleClass('d-none');
});
});
其中“rbtoggle”只是您的单选按钮共享的一个类,而“dvtoggle”是一个用于显示或隐藏 div 的类。
如果您希望最初隐藏 div,请向其中添加类“d-none”。例如
<div class'dvtoggle d-none'>
<!-- other html goes here -->
</div>