我点击按钮后会执行以下代码。它应该从ajax调用中获取一些数据,并决定是继续使用其余代码还是中断。由于某种原因,它不起作用。
$(document).ready(function() {
$( "button#sub1" ).on("click", function(event) {
//event.preventDefault();
//alert("Function temporary disabled");
//return;
var htmlString = $( "#select-result" ).text();
var v=$( "#select-result2" ).text();
var s=$( "#select-result3" ).text();
var c=$( "#select-result7" ).text();
var cont=true;
//alert(v);
//alert(s);
//alert(c);
//alert(htmlString);
//alert(htmlString.indexOf("none"));
//alert(htmlString.indexOf("undefined"));
$.ajax({
type: "GET",
url: "getpending.php",
data: { //getpending.php uses a $_SESSION variable, no need to pass any data here
},
success: function(data)
{
if(data)
{
alert("You have a pending submission.");
cont=false;
}
}
});
$(document).ajaxStop(function () {
if(cont)
{
if(htmlString!="" && htmlString.indexOf("undefined") == -1)
{
//$("button#sub1").attr("disabled", true);
$.ajax({
type: "GET",
url: "calculate.php",
data: { 'raw' : htmlString, 'pts' : v, 'stars' : s, 'cats' : c },
/* success: function(data)
{
alert("success!");
} */
});
$( "#select-result" ).empty();
$( "#select-result2" ).empty();
$( "#select-result3" ).empty();
$( "#select-result7" ).empty();
$( "#sel1" ).text("Submitted for approval");
$(function()
{
setTimeout(function() {
$("#sel1").hide('fade', {}, 500)
}, 2000);
});
}
else if(htmlString=="")
alert("You need to select at least one category to submit...");
else if(htmlString.indexOf("undefined")>-1)
alert("Invalid Selection...");
}
});
$( "#select-result" ).empty();
htmlString.empty();
$( "#select-result2" ).empty();
v.empty();
$( "#select-result3" ).empty();
s.empty();
$( "#select-result7" ).empty();
c.empty();
});
});
</script>
你能发现为什么这不起作用的原因吗? 谢谢!
答案 0 :(得分:0)
能够使用'when'功能解决它:
<script>
$(document).ready(function() {
$( "button#sub1" ).on("click", function(event) {
//event.preventDefault();
//alert("Function temporary disabled");
//return;
var htmlString = $( "#select-result" ).text();
var v=$( "#select-result2" ).text();
var s=$( "#select-result3" ).text();
var c=$( "#select-result7" ).text();
//alert(v);
//alert(s);
//alert(c);
//alert(htmlString);
//alert(htmlString.indexOf("none"));
//alert(htmlString.indexOf("undefined"));
var cont=true;
function aj1() { return $.ajax({
type: "GET",
url: "getpending.php",
data: { },
success: function(data)
{
if(data)
{
alert("You have a pending submission.");
cont=false;
}
}
});
}
$.when(aj1()).done(function() {
if(cont) {
if(htmlString!="" && htmlString.indexOf("undefined") == -1)
{
//$("button#sub1").attr("disabled", true);
$.ajax({
type: "GET",
url: "calculate.php",
data: { 'raw' : htmlString, 'pts' : v, 'stars' : s, 'cats' : c },
/* success: function(data)
{
alert("success!");
} */
});
$( "#select-result" ).empty();
$( "#select-result2" ).empty();
$( "#select-result3" ).empty();
$( "#select-result7" ).empty();
$( "#sel1" ).text("Submitted for approval");
$(function()
{
setTimeout(function() {
$("#sel1").hide('fade', {}, 500)
}, 2000);
});
}
else if(htmlString=="")
alert("You need to select at least one category to submit...");
else if(htmlString.indexOf("undefined")>-1)
alert("Invalid Selection...");
$( "#select-result" ).empty();
htmlString.empty();
$( "#select-result2" ).empty();
v.empty();
$( "#select-result3" ).empty();
s.empty();
$( "#select-result7" ).empty();
c.empty();
}
});
});
});