Javascript 如何在AJAX请求期间禁用锚定标记并更改html()
HTML:Javascript 如何在AJAX请求期间禁用锚定标记并更改html(),javascript,jquery,html,Javascript,Jquery,Html,HTML: 所以一切正常,但现在我想将锚定标记中的html()更改为类似“loading..”的内容,并在ajax请求期间禁用它。使用beforeSend在ajax请求期间执行一些操作,例如: function edit_init(anchor){ $("#edModal").html(""); ref = anchor.href.substr(anchor.href.lastIndexOf('/') + 1); ref = ref.replace(/
所以一切正常,但现在我想将锚定标记中的
html()
更改为类似“loading..”的内容,并在ajax请求期间禁用它。使用beforeSend
在ajax请求期间执行一些操作,例如:
function edit_init(anchor){
$("#edModal").html("");
ref = anchor.href.substr(anchor.href.lastIndexOf('/') + 1);
ref = ref.replace(/^#/, "");
$.post("modal.php",{ajax_get_edModal:1,ajax_modal_for:ref},function(data){
$("#edModal").html(data);
});
}
为了完成任务,您可以对您的功能应用以下更改
$.ajax({
type: 'post',
url: 'example.php',
data: form.serialize(),
beforeSend: function() {
$('.yourTag').html('loading...');
},
success: function(data) {
$('.yourTag').html(data);
},
error: function() {
$('.yourTag').html('error');
}
});
所以,像对模式一样更改html。。。。当我这样做时,ajax代码将无法工作您尝试了什么,我猜您的控制台中出现了语法错误。下面是日志错误:Uncaught TypeError:anchor.html不是edit_initWell的函数,这意味着您将DOM引用视为jQuery
$(anchor.html(“加载…”)。prop(“禁用”,true)
看起来可以工作,但是。。。。实际上,这不是一种形式,这些只是静态值,因此idk关于序列化部分只是一个示例,重要的是在发送之前,忘记$post语法,因为它实际上已被弃用,请使用$ajax syntax我的荣幸@kingraphaII:)我不相信disabled
属性对a
元素有效。您必须在事件上调用preventDefault()
,才能从本质上禁用链接。哦,感谢您刚刚意识到,利用了bootstrap的disabled classI,我同意@TomPietrosanti,所以另一种方法是在调用函数时删除锚定标记的href属性,然后再次将其设置回相同的href
$.ajax({
type: 'post',
url: 'example.php',
data: form.serialize(),
beforeSend: function() {
$('.yourTag').html('loading...');
},
success: function(data) {
$('.yourTag').html(data);
},
error: function() {
$('.yourTag').html('error');
}
});
function edit_init(anchor){
$prevHtml = $(anchor).html();
$href = $(anchor).attr('href');
$(anchor).removeAttr('href');
$(anchor).html('Loading....');
$(anchor).attr('disabled',true);
$("#edModal").html("");
ref = anchor.href.substr(anchor.href.lastIndexOf('/') + 1);
ref = ref.replace(/^#/, "");
$.post("modal.php",{ajax_get_edModal:1,ajax_modal_for:ref},function(data){
$("#edModal").html(data);
$(anchor).html($prevHtml);
$(anchor).removeAttr('disabled');
$(anchor).attr('href',$href);
});
}