我正在使用Ajax调用来检查用户是否已登录。如果没有,请显示登录对话框;否则我希望OnClientClick返回true,以便表单将提交。我正在考虑使用全局变量并检查它,直到Ajax给它一个值...我不确定它是否是正确的方法?谢谢!
(服务器端代码已经运行)
<。>在.ascx中:<asp:Button ID="buttonSubmit" Text="Submit" OnClientClick="return buttonSubmitOnclick()" OnClick="buttonSubmit_Click" runat="server"/>
<。>在.js:
function buttonSubmitOnclick() {
showLogin();
//What to do here??? how to get the result after the ajax call?
}
//========== Ajax ==============
function showLogin() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == "false") {
$find('modalPopupLogin').show(); //Show Login Dialog
};//else is it possible to return a true to buttonSubmitOnclick?
}
}
xmlhttp.open("GET", "AjaxService.aspx?t=auth", true);
xmlhttp.send();
}
答案 0 :(得分:1)
当条件满足时,您可以使用document.forms['myForm'].submit()
在ajax调用的响应中手动提交表单。
按钮按正常按钮(不提交按钮)并在按钮元素中的函数调用之前删除返回,然后执行以下操作:
function buttonSubmitOnclick() {
showLogin();
}
//========== Ajax ==============
function showLogin() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if (xmlhttp.responseText === "false") {
$find('modalPopupLogin').show(); // display the pop up
}else{
document.forms['myForm'].submit() // else submit the form,'myForm' is form id'
}
}
}
xmlhttp.open("GET", "AjaxService.aspx?t=auth", true);
xmlhttp.send();
}