我们说我有:
<form action="myurl.com" method="post" id="myForm">
<input type="number" name="number" class="numberInput">
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
我希望在表单提交之前执行的javascript函数:
function executeBeforeSubmission(){
var form = document.getElementById("myForm");
var inputs = form.getElementsByClassName("numberInput");
for(var i=0; i<inputs.length; i++){
inputs[i].name = "number"+i;
}
}
如何在表单提交之前指定此方法执行?
答案 0 :(得分:0)
您可以使用以下jQuery代码在提交表单之前执行任何操作:
$('#myForm').submit(function() {
// Stuff to do. In your case call the function.
executeBeforeSubmission();
return true; // return false to cancel form action
});