Jquery从文本框中获取值并与复选框进行比较

时间:2013-05-31 06:40:52

标签: jquery

我有两个文本框,如手机号码和电子邮件地址。如果移动电话号码文本框中包含任何值,则会自动选中以下字段。

  • 通讯电子邮件

如果电子邮件地址包含任何值,则选中

  • 移动通信

如何在JQuery

中执行此操作
if $("#RequirementsMobileNumber")==true
{
    $('#CommunicationSMS').attr('checked',true);
}
else if $('#RequirementsEmailAddress')==true
{
    $('#CommunicationEmail').attr('checked',true);
}
else{

}

如果我把上面的代码弄错了。

3 个答案:

答案 0 :(得分:3)

请检查文本框的值,如下所示:

if( $("#RequirementsMobileNumber").val()!='')
{
    $('#CommunicationSMS').attr('checked',true);
else if ($('#RequirementsEmailAddress').val()!='')
{
     $('#CommunicationEmail').attr('checked',true);
}
else{

}

答案 1 :(得分:1)

您需要在括号if中包围()条件。然后检查.val() jQuery函数以获取输入框中的值。

if ($("#RequirementsMobileNumber").val().length > 0) {
    $('#CommunicationSMS').attr('checked', true);
} else if ($('#RequirementsEmailAddress').val().length > 0) {
     $('#CommunicationEmail').attr('checked', true);
} else {

}

答案 2 :(得分:0)

您有语法错误

 if ($("#RequirementsMobileNumber").val()!=''){

    $('#CommunicationSMS').attr('checked',true);
 }
else if ($('#RequirementsEmailAddress').val()!=''){

     $('#CommunicationEmail').attr('checked',true);
} 
else{

}