Javascript - 检查多个字段是否为空不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21134879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:10:05  来源:igfitidea点击:

Javascript - checking if multiple fields are empty not working

javascripthtml

提问by Vladimir

HTML form look:

HTML 表单外观:

<form action="search" id="searchForm" onsubmit="return validateSearch()">
    <input type="text" name="byTitle" pattern="[a-zA-Z0-9]+" placeholder="Enter a word from title">
    <input type="text" name="byAuthor" pattern="[a-zA-Z]+\s[a-zA-Z]+" placeholder="First-name Last-name"> <!--  pattern="\w+ \w+" -->
    <input id="startDate" name="startDate" type="date">
    <input id="endDate" name="endDate" type="date">
    <input type="submit" name="submit" value="Submit">
</form>

Javscript validating function:

Javascript 验证功能:

function validateSearch() {

    var byTitle   = document.getElementById('searchForm').byTitle.value;
    // alert(byTitle);
    var byAuthor  = document.getElementById('searchForm').byAuthor.value;
    // alert(byAuthor);
    var startDate = document.getElementById('searchForm').startDate.value;
    //alert(startDate);
    var endDate   = document.getElementById('searchForm').endDate.value;
    //alert(endDate);       

    if(byTitle == byAuthor == startDate == endDate == ""){
        alert("Enter at least one search parameter!");
        return false;
    }
}

Now, if I leave all form fields empty/unfilled and press submit, alert("Enter at least one search parameter!");message should pop-up, but it doesn't. Wanted to see what are the values of each field I have inserted alertafter each field reading, and they are all same, empty string/blank. So, why is then that if conditionnot seeing them as same, empty fields?

现在,如果我将所有表单字段留空/未填写并按提交,alert("Enter at least one search parameter!");消息应该弹出,但它没有。想看看我alert在每个字段读取后插入的每个字段的值是什么,它们都是相同的,空字符串/空白。那么,为什么if condition不将它们视为相同的空白字段?

回答by Dalorzo

Your issue is here:

你的问题在这里:

wrong

错误的

if(byTitle == byAuthor == startDate == endDate == ""){

correct

正确的

if(byTitle === "" && byAuthor ==="" && startDate ==="" && endDate === ""){

Javascript has a simpler way to evaluate if a value is null, empty string or 0 by only evaluating the variable:

Javascript 有一种更简单的方法来通过仅评估变量来评估值是 null、空字符串还是 0:

if(!byTitle  && !byAuthor && !startDate && !endDate ){ 

Another advise I wanted to share is to use "===" instead of "==".

我想分享的另一个建议是使用“===”而不是“==”。

回答by James Donnelly

""is falsy, so you can simply use:

""假的,所以你可以简单地使用:

if (!byTitle && !byAuthor && !startDate && !endDate) {
    ...
}

This uses the logical NOT operatoron your variables (!) to convert them to boolean values, then return the opposite.

这在变量 ( )上使用逻辑 NOT 运算符!将它们转换为布尔值,然后返回相反的值。

JSFiddle demo.

JSFiddle 演示

回答by Onur Sevik

maybe you can like this;

也许你会喜欢这个;

$(function(){
    var bytitle = $("input[name=byTitle]").val();
    var byAuthor = $("input[name=byAuthor]").val();
    var startDate = $("input[name=startDate]").val();
    var endDate = $("input[name=endDate]").val();

    if( bytitle == '' && byAuthor == '' && startDate == '' && endDate == '')
        alert("Enter at least one search parameter!");
});

回答by beno?t

You can use ANDin your condition :

您可以在您的条件下使用AND

byTitle = "";
byAuthor = "";
startDate =""; 
endDate = ""
if(byTitle == "" && byAuthor =="" && startDate =="" && endDate == ""){
        alert("Enter at least one search parameter!");
        return false;
    }

http://jsfiddle.net/tCJ95/

http://jsfiddle.net/tCJ95/

回答by Favouronu

Try this:

试试这个:

var ee = byTitle = byAuthor = startDate = endDate="";
if(ee=="null" || ee.length==0 || !ee)
{
    alert("all fields must be filled out");
    return false;
}