【发布时间】:2014-07-31 12:51:44
【问题描述】:
我正在使用下面的 jquery 进行表单验证,下面的代码在 IE9 中运行良好,但在 safari 中有时它只能在第二次单击按钮时运行。
例如,
我正在输入正确的电子邮件和错误的密码,它显示错误消息,然后我更正密码并单击按钮,错误被删除但表单没有提交,然后我再次单击按钮,它正在提交。这仅发生在野生动物园中。我正在使用 Safari 5.1
我认为单个就绪函数就足够了,但我使用了 2 个就绪函数,我应该在不影响现有功能的情况下删除其中一个。
jQuery().ready(function domReady($) {
$('#txtLogin_Id').removeClass('outLineRed');
$('#txtPassword').removeClass('outLineRed');
$('#spnLoginErr').hide();
$('#txtLogin_Id').watermark("Email Address");
$('#txtPassword').watermark("Password");
if ($('#lblLoginErr').val() != '') {
$('#spnLoginErr').show();
$('#spnLoginErr').html($('#lblLoginErr').val());
$('#MsgSucc').attr("style", "background-color:White;!important;");
}
else {
$('#spnLoginErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
}
$('#txtLogin_Id').blur(function () {
if ($('#txtLogin_Id').val() == '') {
$('#txtLogin_Id').addClass('outLineRed');
$('#spnLoginErr').show();
$('#spnLoginErr').html("Please enter email address.");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnLoginErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#txtLogin_Id').removeClass('outLineRed');
}
}
);
$('#txtPassword').blur(function () {
if ($('#txtPassword').val() == '') {
$('#txtPassword').addClass('outLineRed');
$('#spnLoginErr').show();
$('#spnLoginErr').html("Password has to be more than 6 characters");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnLoginErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#txtPassword').removeClass('outLineRed');
}
}
);
$('#BtnLogin').click(function () {
if ($('#txtPassword').val() == '' && $('#txtLogin_Id').val() == '') {
$('#txtLogin_Id').addClass('outLineRed');
$('#txtPassword').addClass('outLineRed');
$('#spnLoginErr').show();
$('#spnLoginErr').html("Please enter email address and password.");
$('#MsgSucc').attr("style", "background-color:White;!important;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
}
if ($('#txtLogin_Id').val() == '') {
$('#txtLogin_Id').addClass('outLineRed');
$('#spnLoginErr').show();
$('#spnLoginErr').html("Please enter email address.");
$('#MsgSucc').attr("style", "background-color:White;!important;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#txtLogin_Id').removeClass('outLineRed');
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
}
if ($('#txtPassword').val() == '') {
$('#txtPassword').addClass('outLineRed');
$('#spnLoginErr').show();
$('#spnLoginErr').html("Password has to be more than 6 characters");
$('#MsgSucc').attr("style", "background-color:White;!important;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#txtPassword').removeClass('outLineRed');
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
}
});
(function errorMsgBasedhighlight() {
if ($('#lblLoginErr').val() == 'Please enter a valid email address and password.') {
$('#txtPassword').addClass('outLineRed');
$('#txtLogin_Id').addClass('outLineRed');
}
else if ($('#lblLoginErr').val() == 'Please enter a valid email address.') {
$('#txtLogin_Id').addClass('outLineRed');
}
else if ($('#lblLoginErr').val() == 'Please enter a valid Password.') {
$('#txtPassword').addClass('outLineRed');
}
else {
$('#txtLogin_Id').removeClass('outLineRed');
$('#txtPassword').removeClass('outLineRed');
}
})($);
$('#txtLogin_Id').blur(function () {
if ($('#txtLogin_Id').val() == '') {
$('#txtLogin_Id').addClass('outLineRed');
return false;
}
else {
$('#lblLoginErr').hide();
$('#txtLogin_Id').removeClass('outLineRed');
}
}
);
$('#txtLogin_Id').blur(function () {
if ($('#txtPassword').val() == '') {
$('#txtPassword').addClass('outLineRed');
return false;
}
else {
$('#lblLoginErr').hide();
$('#txtPassword').removeClass('outLineRed');
}
}
);
});
【问题讨论】:
-
我听说,如果我删除 jquery domready,我会工作得很好,是这样吗?我尝试删除它,但遇到了其他问题
标签: javascript jquery safari