我试图在给定页面上的所有表单上捕获提交事件(这是动态的)
到目前为止我得到了什么:
/**
* Capture form submits
*/
function addEvent(evnt, elem, func) {
if (elem.addEventListener) { // W3C DOM
elem.addEventListener(evnt,func,false);
} else if (elem.attachEvent) { // IE DOM
elem.attachEvent("on"+evnt, func);
} else { // No much to do
elem[evnt] = func;
}
}
var formsCollection = document.forms;
for(var i=0;i<formsCollection.length;i++) {
//alert(formsCollection[i]);
var form = document.forms[i];
addEvent('submit', form, function(e) {
var validated = true;
// do form validation
alert(i);
if (!validated) {
e.preventDefault();
}
return validated;
});
}
问题是这只捕获了最后一种形式。
有任何想法吗?