Jquery 如何防止与Ajax结果相关的默认事件
我有一个按钮,执行两个任务,一个是主题的一部分,另一个是ajax调用 这是默认行为:Jquery 如何防止与Ajax结果相关的默认事件,jquery,Jquery,我有一个按钮,执行两个任务,一个是主题的一部分,另一个是ajax调用 这是默认行为: $(".next").click(function () { if (animating) return false; animating = true; current_fs = $(this).parent(); next_fs = $(this).parent().next(); //activate next step on progressbar using
$(".next").click(function () {
if (animating) return false;
animating = true;
current_fs = $(this).parent();
next_fs = $(this).parent().next();
//activate next step on progressbar using the index of next_fs
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
//show the next fieldset
next_fs.show();
//hide the current fieldset with style
current_fs.animate({ opacity: 0 }, {
step: function (now, mx) {
//as the opacity of current_fs reduces to 0 - stored in "now"
//1. scale current_fs down to 80%
scale = 1 - (1 - now) * 0.2;
//2. bring next_fs from the right(50%)
left = (now * 50) + "%";
//3. increase opacity of next_fs to 1 as it moves in
opacity = 1 - now;
current_fs.css({ 'transform': 'scale(' + scale + ')' });
next_fs.css({ 'left': left, 'opacity': opacity });
},
duration: 800,
complete: function () {
current_fs.hide();
animating = false;
},
//this comes from the custom easing plugin
easing: 'easeInOutBack'
});
});
现在,所有这些行为都必须针对我在另一个.js中的ajax调用:
$("#ToStepThree").click(function () {
var valueParameter = $("#DocumentNumber").val();
$.ajax({
cache: false,
url: 'Customer/ExistUserByDocument',
type: 'GET',
data: { document: valueParameter },
dataType: 'json',
success: function (exists)
{
alert(exists);
},
error: function (xhr, ajaxOptions, thrownError)
{
alert("error");
}
});
});
所以这里的想法是,如果这个Ajax返回true,我需要重写下一个的行为,但是如果它是false,那么它应该转到下一个字段集
这是HTML:
<input type="button" id="ToStepThree" name="next" class="next action-button" value="Continue →" />
知道如何实现这一点吗?为什么不能在基于响应值的ajax成功回调中设置
动画
?正如@charlietfl sais所说,如果ajax响应为true,则只需调用第一个函数,因此,您应该做的第一件事是将该函数分离为一个命名函数,并根据ajax结果调用它。您的意思是将下一次单击封装到函数中?但是如果我这样做了,其他的按钮就不能用class.next了,除非我调用了正确的函数?