在ajax执行完成功能之前,有没有办法暂停javascript?
function validateinput (name, parent, access)
{
//we assume that the input does not exist
exists = false;
//validate with server, expected response is true or false
$.post("AJAXURL", {action: "category", name: name, parent: parent},
function (data, textStatus, xhr)
{
//if returned true, the entry does exist for that parent
if (data == "true")
{
alert("AJAX");
exists = true;
}
else
exists = false;
});
switch (true)
{
//If the name field is blank (still displaying hint)
case (name == 'Subcategory Name' || name == 'Category Name'):
alert("Please enter a name");
break;
//if the name already exists for that parent
/*****/ case (exists == true):
alert("SWITCH");
break;
//if the parent field is displaying hint, assume parent is blank
case (parent == 'Parent Category'):
parent = '';
break;
//if the usergroup field is blank, assume value of parent
case (access == 'UserGroup Allowed To View'):
//if parent is also blank, assume lowest level usergroup access
if (parent == '')
access = "UserGroupTicketWorkers";
else
$.post("AJAXURL", {action: "specificcat", name: parent},
function (data, textStatus, xhr)
{
access = data['access'];
}
);
break;
default:
return true;
}
return false;
}
alert("SWITCH");
之前收到alert("AJAX");
(第二种情况的条件当前是正确的,我在调试中将其反转以弄清楚发生了什么)我使用了异步设置,它就像一个魅力。只需将我的代码从$.post()
更改为$.ajax()
并将其设置为发布
$.ajax(
{
url: "plugins/actions/edittickets/libs/changetickets.php",
type: 'POST',
data:
{
action: "category",
name: name,
parent: parent
},
async: false,
success: function (data, textStatus, xhr)
{
//if returned true, the entry does exist for that parent
if (data == "true")
exists = true;
}
});
答案 0 :(得分:1)
尝试返回exists
,如下所示。
function validateinput (name, parent, access)
{
//we assume that the input does not exist
var exists = false;
//validate with server, expected response is true or false
$.post("AJAXURL", {action: "category", name: name, parent: parent},
function (data, textStatus, xhr)
{
//if returned true, the entry does exist for that parent
if (data == "true")
{
alert("AJAX");
exists = true;
}
else
exists = false;
switch (true)
{
//If the name field is blank (still displaying hint)
case (name == 'Subcategory Name' || name == 'Category Name'):
alert("Please enter a name");
break;
//if the name already exists for that parent
case (exists == true):
alert("SWITCH");
break;
//if the parent field is displaying hint, assume parent is blank
case (parent == 'Parent Category'):
parent = '';
break;
//if the usergroup field is blank, assume value of parent
case (access == 'UserGroup Allowed To View'):
//if parent is also blank, assume lowest level usergroup access
if (parent == '')
access = "UserGroupTicketWorkers";
else
$.post("AJAXURL", {action: "specificcat", name: parent},
function (data, textStatus, xhr) {
access = data['access'];
});
break;
default:
exists = true;
}
});
return exists;
}
答案 1 :(得分:0)
如果您在参数列表中添加'async':false
,那么您可以通过同步方式发送AJAX查询。
有关详细信息,请参阅文档:http://api.jquery.com/jQuery.ajax/
但正如其他人在评论中指出的那样,这不是切换工作的方式,因此你仍然需要理解switch语句并更改相应的代码。有关详细信息,请参阅此处:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch