我有两个文本框,如手机号码和电子邮件地址。如果移动电话号码文本框中包含任何值,则会自动选中以下字段。
如果电子邮件地址包含任何值,则选中
如何在JQuery
,
if $("#RequirementsMobileNumber")==true
{
$('#CommunicationSMS').attr('checked',true);
}
else if $('#RequirementsEmailAddress')==true
{
$('#CommunicationEmail').attr('checked',true);
}
else{
}
如果我把上面的代码弄错了。
答案 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{
}