2014-10-01 48 views
1
$('#fastbill').click(function(){ 
    if ($('#changevaluebill').attr('checked','true')){ 
     $('#changevaluebill').attr("value","Recharge Now"); 
    } 
    else { 
     $('#changevaluebill').attr("value","Proceed"); 
    } 
}); 

复选框条件不起作用。基于复选框的条件功能是否被检查

+0

'$( '#changevaluebill')。VAL(函数(){ 回报this.checked?“立即充值':'继续' });' – 2014-10-01 13:11:27

回答

2

有很多方法来检查,如果无论是复选框被选中

您可以使用is(':checked')

尝试:

$('#fastbill').click(function(){ 
    if($('#changevaluebill').is(':checked')){ 
    $('#changevaluebill').val("Recharge Now"); 
    } else { 
    $('#changevaluebill').val("Proceed"); 
    } 
}); 

其他方式可以是:

$('#changevaluebill').prop('checked') //布尔
$('#changevaluebill:checked').length // Integer> 0
$('#changevaluebill:checked').size() // .size()可用于代替的。长度
$('#changevaluebill').get(0).checked //布尔真
$('#changevaluebill')[0].checked //布尔真(与上述相同)

Reference

+0

thnxxx很多....... itz工作 – 2014-10-01 13:31:29

1

此没有做你在想什么:

$('#changevaluebill').attr('checked','true') 

这并不如果值是确定0,它设置的值为'true'。您可以使用the .is() function有条件地检查元素的某个方面。事情是这样的:

$('#changevaluebill').is(':checked') 
+0

我试过了,但它也没有工作 – 2014-10-01 13:12:00

+0

@muqeemkhan:你能更具体吗?当你在浏览器中调试它时,它在哪里以及如何明确地失败?代码是否被调用? jQuery选择器能够找到你期望的元素吗?逻辑如何进展? – David 2014-10-01 13:17:23

1

正如其他人所说,你需要可以使用.is():checked

但是,你应该使用.VAL()来设置输入字段的值

$('#fastbill').click(function() { 
    $('#changevaluebill').val(function() { 
     return this.checked ? 'Recharge Now' : 'Proceed' 
    }); 
}); 
相关问题