“多重”;及;javascript中的条件
在这种情况下,我需要在JavaScript函数中使用三个和条件 我已经写了这样的代码“多重”;及;javascript中的条件,javascript,Javascript,在这种情况下,我需要在JavaScript函数中使用三个和条件 我已经写了这样的代码 if (( frm_add_announcement.sublink[0].checked == true )) { if ((document.getElementById('captionurl').value.trim() == "") && ( document.getElementById("captionfile").files.length == 0 )
if (( frm_add_announcement.sublink[0].checked == true )) {
if ((document.getElementById('captionurl').value.trim() == "") &&
( document.getElementById("captionfile").files.length == 0 ) &&
(document.getElementById('ifpresent').value != "1")) {
alert("Enter a url/upload a file");
//var alertmsg=document.getElementById("captionfile").value;
//alert(alertmsg);
document.getElementById('captionurl').focus();
return false;
}
}
但它不起作用。没有控制台错误您的代码没有问题,也没有错误,但我发现您可以通过创建变量进行重构,而不是执行2个
语句,如果
语句,则只能执行一个:
var captionurl = document.getElementById('captionurl'),
captionfile = document.getElementById('captionfile'),
ifpresent = document.getElementById('ifpresent');
if (frm_add_announcement.sublink[0].checked &&
captionurl.value.trim() === '' &&
captionfile.files.length === 0 &&
ifpresent.value !== '1') {
alert('Enter a url/upload a file');
//var alertmsg=captionfile.value;
//alert(alertmsg);
captionurl.focus();
return false;
}
请注意,“但它不工作”不表示检查结果是什么您的代码在语法上看起来很好。您需要在需要的地方调用
函数
,以防错过此部分。另一个可能的问题是,您可能需要检查captionurl
中是否有值,文件数是否大于0,以及ifpresent
值是否为1。在这种情况下,您需要具备三个或三个条件:
if (( frm_add_announcement.sublink[0].checked == true )) {
if ((document.getElementById('captionurl').value.trim() == "") ||
( document.getElementById("captionfile").files.length == 0 ) ||
(document.getElementById('ifpresent').value != "1")) {
alert("Enter a url/upload a file");
//var alertmsg=document.getElementById("captionfile").value;
//alert(alertmsg);
document.getElementById('captionurl').focus();
return false;
}
}
您可能还想否定您的第一个条件。控制台中是否有错误?您应该逐个检查每个条件。@KeesvanLierop-No。比如:
var c1=document.getElementById('captionurl').value.trim();控制台日志(c1)代码>等等。我认为您最好针对每个条件进行测试。“它不起作用”并没有告诉我们它应该起什么作用:)