当我需要检查你的内容是否为空时,如果它是空的,那么就会回来并给我成功。
它绝不会向前发展,并告诉我内容不足有缺陷。
即使我有空,电子邮件,密码,名字,姓氏和年龄,问题也只是我的警报。
$(function () {
$('#clickUserCreate').click(function() {
var Email = $.trim($('#Email').val(null));
var Password = $.trim($('#passwordVal').val(null));
var FirstName = $.trim($('#Firstname').val(null));
var LastName = $.trim($('#Lastname').val(null));
var Age = $.trim($('#Age').val());
//Check
if(Email == "" && Password == "" && FirstName == "" && LastName == "" && Age == "")
{
//error here
$('.OutError').html('Please enter all fields.');
}
else
{
//succes her
//The only show it here!
alert("Succes");
}
})
})
答案 0 :(得分:1)
当您执行var Email = $.trim($('#Email').val(null));
之类的操作时,您将Email
设置为null
。您的逻辑检查Email == ""
失败,因为空值不等于空字符串。此外,您还要检查所有输入字段是否为空。如果某些(但不是全部)字段为空,您仍将获得成功。请尝试以下方法。它将始终返回错误,直到填写所有字段。
$(function () {
$('#clickUserCreate').click(function() {
var Email = $.trim($('#Email').val());
var Password = $.trim($('#passwordVal').val());
var FirstName = $.trim($('#Firstname').val());
var LastName = $.trim($('#Lastname').val());
var Age = $.trim($('#Age').val());
//Check
if(Email == "" || Password == "" || FirstName == "" || LastName == "" || Age == "")
{
//error here
$('.OutError').html('Please enter all fields.');
}
else
{
//succes her
//The only show it here!
$('.OutError').html('');
alert("Succes");
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="OutError"></p>
<input type="text" id="Email" />
<input type="text" id="passwordVal" />
<input type="text" id="Firstname" />
<input type="text" id="Lastname" />
<input type="text" id="Age" />
<button id="clickUserCreate">Create User</button>