html的一部分被替换为ajax调用的结果。在测试期间,我让ajax调用与被替换的完全相同的东西。问题是,我没有应用于旧HTML的JQuery UI和“onClick”材料适用于新的。我试着回想一下再次应用它的功能,但它不起作用。奇怪的是,如果我直接将它输入到谷歌浏览器的建议中(新的Ajax-html受JQuery命令影响),它会起作用。JQuery对新元素没有影响
这是我的代码,其中包括HTML的在开始(工作)原设置和第二设置(所有需要的元素已经被实例化后,它来了。):
function set_up() { // <-- if I call this function in the consul, it works.
$('.delete-button').click(function() {
var confirm = window.confirm("Are you sure you want to delete? After you save, this file will not be recoverable.")
if (confirm){
$(this).parent('.deletable-group').remove()
}
});
$('.field-default[data-multi="True"]').makeMulti();
$("input.field-input-date").datepicker();
alert("HERE!") //Is reached both times I call the function. So the code above is executing.
}
var options = {
target: '.server-response-box',
success: function() { //<--- Gets called when the ajax call goes through.
$('.needs-update').each(function(index) {
url = "/field/1/" + $('.edit-wrapper').attr('data-entry-ID') + "/" + $(this).attr('data-field-id');
old_this = this
$.get(url,function(data){
$(old_this).replaceWith(data);
});
});
set_up(); // Tries to set everything up again.
},
beforeSubmit: function(arr, $form, options) {
}
};
$('#form').ajaxForm(options);
set_up(); //<-- sets it all up the first time.
使用'$(文件)。在( '点击', '.delete按钮',函数使用'.on()'将一个click事件绑定到所有当前和将来的'.delete-button'元素上,除非你想让整个'set_up()'再次运行, case使用Brad M的解决方案 – Joe
它看起来像是在替换发生之前调用set_up() –