[英]javascript if condition on checkbox functioning only once
I am fairly new to javascript programming and just can't seem to get this right even after going through the questions here, basically i have a checkbox which displays two radio buttons only when it is unselected 我对javascript编程相当陌生,即使经过这里的问题,似乎也无法正确解决这个问题,基本上我有一个复选框,仅当未选中它时才显示两个单选按钮
<span><strong>Not Available</strong></span>
<input type="checkbox" placeHolder="" id="available" style="" name="available" required value="Yes"/>
<span style=" visibility: hidden; " id="BACSSpan">BACS</span><input type="radio" id="BACS" name="paymentSpecified" value="BACS" style="visibility: hidden;">
<span style="visibility: hidden;" id="ChequeSpan">Cheque</span> <input type="radio" id="Cheque" name="paymentSpecified" value="Cheque" style=visibility: hidden;">
I have used the following javascript: 我已使用以下javascript:
$('body').on('click', '#available', function ()
{
if ($('#available:checked'))
{
$('#BACSSpan').css('visibility','hidden');
$('#BACS').css('visibility','hidden');
$('#ChequeSpan').css('visibility','hidden');
$('#Cheque').css('visibility','hidden');
} else
{
$('#BACSSpan').css('visibility','visible');
$('#BACS').css('visibility','visible');
$('#ChequeSpan').css('visibility','visible');
$('#Cheque').css('visibility','visible');
}
})
What it should do is alternate between showing the two radio buttons, but it is not doing anything, but when i reverse the order of the condition ie opposite of what i want; 它应该做的是显示两个单选按钮之间的交替,但是它什么也没做,但是当我颠倒条件的顺序时,即与我想要的相反。 then the required functionality happens once only 然后所需的功能只会发生一次
The thing is that $('#available:checked')
is a jQuery object - not a bool value. 问题是$('#available:checked')
是一个jQuery对象-不是布尔值。 To get bool value you should do: 要获得布尔值,您应该执行以下操作:
if ($(this).is(':checked')) {
...
}
else {
...
}
If you don't want to change your code a lot, you can simply add .length
. 如果您不想太多更改代码,只需添加.length
。
if ($('#available:checked').length) {
//Do Something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.