我有一个HTML文档,带有jquery代码
$(function(){
[...]
$("#newNotice").click(function(){
$("#properties").load("testDiagramm.html #NoticeForm");
return false;
});
function showFormValues(){
var s = $("form").serialize();
[... Do things ...]
}
$("input, textarea").change(showFormValues);
});
一开始,HTML文档中没有任何表单。但我将不同的表单加载到文档中。其中一个请求你可以在这里看到
$("#properties").load("testDiagramm.html #NoticeForm");
问题是。代码行
$("input, textarea").change(showFormValues);
仅在开始时加载表单时才触发。我必须做什么,如果我想执行showFormValues()函数,当我在公式中更改某些内容时,我将在以后加载?
非常感谢您的回答。
拉拉
答案 0 :(得分:3)
您的表单在通过ajax重新加载后失去了对DOM的绑定,因此先前绑定到注入页面的元素的事件处理程序将丢失。
我通常会建议将事件委派与live一起使用,但它不支持更改事件,因此安全的做法是使用回调作为$ .load函数的第二个参数重新绑定:
$(function(){
[...]
$("#newNotice").click(function(){
$("#properties").load("testDiagramm.html #NoticeForm", function() {
$("input, textarea").change(showFormValues);
});
return false;
});
function showFormValues(){
var s = $("form").serialize();
[... Do things ...]
}
$("input, textarea").change(showFormValues);
});