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
Javascript - checking if multiple fields are empty not working
提问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 alert
after each field reading, and they are all same, empty string/blank. So, why is then that if condition
not 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 运算符!
将它们转换为布尔值,然后返回相反的值。
回答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
回答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;
}