获取用javascript提交的表单的实例
在提交之前,我想获得提交表单的句柄获取用javascript提交的表单的实例,javascript,forms,hidden-field,onsubmit,Javascript,Forms,Hidden Field,Onsubmit,在提交之前,我想获得提交表单的句柄 页面中可能有多个表单 我不知道表单名称/id 原因:在模板级提交表单之前,我想做一些修改。使用jQuery时,应该是这样的: $(function() { $('form').submit(function() { // the code goes here; // variable `this` is an instance of form alert($(this).className); }); }); $("form"
原因:在模板级提交表单之前,我想做一些修改。使用jQuery时,应该是这样的:
$(function() {
$('form').submit(function() {
// the code goes here;
// variable `this` is an instance of form
alert($(this).className);
});
});
$("form").submit(function(e) {
console.log("Form ID that is being submit %s",$(this).attr("id"));
});
for (var i=0; i < document.forms.length; i++){
document.forms[i].onSubmit = function(){
// logic goes here;
// document.forms[i] is the instance of form
if (formIsHappy()){
return true; //form submits
}else{
return false; //prevents the submit
}
};
}
如果使用jQuery,您可以考虑执行以下操作:
$(function() {
$('form').submit(function() {
// the code goes here;
// variable `this` is an instance of form
alert($(this).className);
});
});
$("form").submit(function(e) {
console.log("Form ID that is being submit %s",$(this).attr("id"));
});
for (var i=0; i < document.forms.length; i++){
document.forms[i].onSubmit = function(){
// logic goes here;
// document.forms[i] is the instance of form
if (formIsHappy()){
return true; //form submits
}else{
return false; //prevents the submit
}
};
}
在纯javascript中,您可以通过执行document.getElementsByTagName(“表单”)并循环获得的数组来实现类似的功能。如果没有jQuery,情况会是这样的:
$(function() {
$('form').submit(function() {
// the code goes here;
// variable `this` is an instance of form
alert($(this).className);
});
});
$("form").submit(function(e) {
console.log("Form ID that is being submit %s",$(this).attr("id"));
});
for (var i=0; i < document.forms.length; i++){
document.forms[i].onSubmit = function(){
// logic goes here;
// document.forms[i] is the instance of form
if (formIsHappy()){
return true; //form submits
}else{
return false; //prevents the submit
}
};
}
for(var i=0;i
您是否在使用某种JS框架(jQuery、Prototype、MooTools)?