我在经典的ASP页面上有一个JQuery模式弹出窗体。它设置为用户单击链接以打开表单。他们输入他们的电子邮件地址,然后提交表格。我想做的是检查电子邮件是否在我的数据库中,如果不是,请重新加载模态弹出窗体并显示错误消息。
表单工作正常,但我不知道如果没有为此设置另一个弹出窗口找不到电子邮件,如何再次弹出该表单。我刚刚开始使用JQuery,很抱歉,如果这是一个简单的问题。我环顾四周,但没有找到任何这方面的例子。
这是JQuery代码:
<script type="text/javascript">
$(function () {
//Modal pop-up form
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$("#dialog:ui-dialog").dialog("destroy");
var email = $("#email"),
allFields = $([]).add(email);
tips = $(".validateTips");
function updateTips(t) {
tips
.text(t)
.addClass("ui-state-highlight");
setTimeout(function () {
tips.removeClass("ui-state-highlight", 1500);
}, 500);
}
function checkLength(o, n, min, max) {
if (o.val().length > max || o.val().length < min) {
o.addClass("ui-state-error");
updateTips("Length of " + n + " must be between " +
min + " and " + max + ".");
return false;
} else {
return true;
}
}
function checkRegexp(o, regexp, n) {
if (!(regexp.test(o.val()))) {
o.addClass("ui-state-error");
updateTips(n);
return false;
} else {
return true;
}
}
$("#dialog-form").dialog({
autoOpen: false,
height: 350,
width: 350,
modal: true,
buttons: {
"Send Login": function () {
var bValid = true;
allFields.removeClass("ui-state-error");
bValid = bValid && checkLength(email, "email", 5, 80);
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please enter a valid email address.");
if (bValid) {
$("#users tbody").append("<tr>" +
"<td>" + email.val() + "</td>" +
"</tr>");
$(this).dialog("close");
$("form[name='modalPopup']").submit()
}
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
allFields.val("").removeClass("ui-state-error");
}
});
$("#send-password")
.click(function () {
$("#dialog-form").dialog("open");
});
});
</script>
以下是打开表单的链接:
<strong>Forgot your login?:</strong> <a href="#" id="send-password">click here</a></p>
以下是表格:
<div id="dialog-form" title="Send My Login">
Enter the email address associated with your account. Your password will be sent to you.
<br />
<strong>If you do not have an email address</strong>, or do not
remember it, please contact your Human Resources department to have your login
provided to you.
<p class="validateTips"></p>
<form action="SendLogin.asp" method="Post" name="modalPopup">
<fieldset>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form>
</div>
非常感谢您的帮助!
答案 0 :(得分:0)
首先关闭对话框是否有特殊原因?
我要做的是保持对话框打开并使用$.post()
所以你的按钮处理程序会变成这样:
if (bValid) {
$("#users tbody").append("<tr>" +
"<td>" + email.val() + "</td>" +
"</tr>");
$.post('SendLogin.asp',$("form[name='modalPopup']").serialize(),function(data){
if (data.success)
{
$(this).dialog("close");
// do some further action..
}
else
{
// tell the user about the problem
}
}, 'json');
}
注意:我将'json'作为第4个参数传递给$.post()
。这会将响应处理程序中的data
参数自动转换为js对象,但显然会从服务器返回一些json数据。您可能需要对此进行调整以使用经典的asp。
注2:Firebug是您调试ajax请求的朋友!
您可以阅读此处使用的功能:
答案 1 :(得分:0)
好的,所以我能够实现上述评论中描述的目标。我添加了一个javascript查询字符串函数:
function getQuerystring(key, default_) {
if (default_ == null) default_ = "";
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null)
return default_;
else
return qs[1];
}
然后在我的弹出参数中调用它:
$("#dialog-form").dialog({
autoOpen: getQuerystring('loginform'),
height: 350,
width: 350,
modal: true,.....
现在如果查询字符串为“true”,它将autoOpen
,否则它将为“false”并且仅在单击时打开。这应该对我有用,但如果有更多有经验的成员有更好的建议,我很乐意听。