我将Jquery与ajax一起使用。 我在点击,取消绑定后和ajax绑定后点击。 但是绑定方法不起作用。 请帮帮我
$(".lang").on("click", function (e) {
$(this).unbind('click');
if ($(this).hasClass("disabledLanguage")) {
return false;
}
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
type: "POST",
url: Host + "Language",
data: { __RequestVerificationToken: token, Code: $(this).data("code") },
success: function (data) {
if (data.IsCompleted) {
//window.location = window.location.href;
}
else {
//alert("Error");
}
$(this).unbind('click'); //<----Notworking
}
});
return false;
});
答案 0 :(得分:1)
/**
* make a function to handle the click event stand alone
* it's good for reuse and test
*/
function clickHandler(e){
/**
* cache the result of $(this)
* for performance and reuse in another function context
*/
var $lang = $(this);
$lang.unbind('click', clickHandler);
if ($lang.hasClass("disabledLanguage")) {
return false;
}
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
type: "POST",
url: Host + "Language",
data: { __RequestVerificationToken: token, Code: $lang.data("code") },
success: function(data) {
if (data.IsCompleted) {
//window.location = window.location.href;
} else {
//alert("Error");
}
/**
* doing after click,unbind and after ajax bind click
* so it should be bind click but not unbind click
*/
$lang.bind('click', clickHandler);
}
});
return false;
}
$(".lang").on("click", clickHandler);
可能这可以帮到你,并在评论中提供更多细节