[英]Check for values before jQuery.ajax$ request
The script I have is working good when executed, and it returns the server side error as expected. 我执行的脚本运行良好,并且按预期返回服务器端错误。
I would like to learn how to use jquery to check if the values (userid, password) to be passed is empty, before the script runs the .$ajax request, and only get to that part if both values are filled in. 我想学习如何使用jquery在脚本运行。$ ajax请求之前检查要传递的值(用户ID,密码)是否为空,并且只有在两个值都填写的情况下才能到达该部分。
This is the jQuery 这是jQuery
var userid = $(".YourOrder_MainLoginEmail").val();
var password = $(".YourOrder_MainLoginPassword").val();
var url = "/ShoppingCart/ShoppingCartLogin";
$.ajax({
url: url,
type: "POST",
dataType: "json",
data: { userId: userid, pass: password },//{ userId: userid, pass: password },
cache: false,
success: function (result) {
if (result.success == "Valid") {
// hide the login form and clear and hide error
$('.YourOrder_loginError').text('');
$('.YourOrder_loginError').css('visibility', 'hidden');
$('.YourOrder_loginForm').css('visibility', 'hidden');
// show the shipping address section
$('.YourOrder_ShipAddress').css('visibility', 'visible');
location.reload();
}
if (result.error == "Invalid") {
// hide shipping address section
$('.YourOrder_ShipAddress').css('visibility', 'hidden');
$('.YourOrder_loginError').css('visibility', 'visible');
$('.YourOrder_loginError').text('The user name or password provided is incorrect.');
}
}
});
Thank you very much for your help. 非常感谢您的帮助。 - itortu. -itortu。
EDIT 编辑
Can I get help making the check a bit better? 我可以得到帮助,使支票变得更好吗?
This is what is happening: 这是正在发生的事情:
1- yes email, no password : password message error enter password, no email: enter email error, but password error remains even with password value in it, so in the end both messages are shown. 1-是的电子邮件,没有密码:密码消息错误,输入密码,没有电子邮件:输入电子邮件错误,但是即使密码值仍然存在,密码错误仍然存在,因此最后都显示了这两个消息。
2- yes password, no email : email message error enter email, no password: enter password error, but email error remains even with email value in it, so in the end both messages are shown. 2-是,密码,不输入电子邮件:电子邮件错误输入电子邮件,不输入密码:输入密码错误,但是即使其中包含电子邮件值,电子邮件错误仍然存在,因此最后都显示了这两个消息。
how can this change to only show/ hide pertinent messages and avoid having both displaying after the value for the first error message has been filled? 如何更改为仅显示/隐藏相关消息,并避免在填充第一个错误消息的值之后都显示它们?
JQuery jQuery查询
if (userid == "") {
$('.YourOrder_loginError').css('visibility', 'visible');
$('.YourOrder_loginError').text('Enter your e-mail address.');
return false
}
if (password == "") {
$('.YourOrder_loginError_password').css('visibility', 'visible');
$('.YourOrder_loginError_password').text('Enter your password.');
return false
}
Many thanks for your help. 非常感谢您的帮助。
Ehh, just check them? 恩,只是检查一下?
if (userid == "" || password == "") { return false }
//AJAX and so on.. script will jump out if either userid or password is empty
var userid = $(".YourOrder_MainLoginEmail").val();
var password = $(".YourOrder_MainLoginPassword").val();
var url = "/ShoppingCart/ShoppingCartLogin";
if(userid=='' || password=='' ){
alert("Fill in complete details");
}
else {
$.ajax({
url: url,
type: "POST",
dataType: "json",
data: { userId: userid, pass: password },//{ userId: userid, pass: password },
cache: false,
success: function (result) {
if (result.success == "Valid") {
// hide the login form and clear and hide error
$('.YourOrder_loginError').text('');
$('.YourOrder_loginError').css('visibility', 'hidden');
$('.YourOrder_loginForm').css('visibility', 'hidden');
// show the shipping address section
$('.YourOrder_ShipAddress').css('visibility', 'visible');
location.reload();
}
if (result.error == "Invalid") {
// hide shipping address section
$('.YourOrder_ShipAddress').css('visibility', 'hidden');
$('.YourOrder_loginError').css('visibility', 'visible');
$('.YourOrder_loginError').text('The user name or password provided is incorrect.');
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.