The first way: form submission, add onsubmit event in form label to determine whether form submission is successful
?
12345678910111213141516171819 |
<script type=
"text/javascript"
>
function validate(obj) {
if (confirm(
"提交表单?"
)) {
alert(obj.value);
return true
;
}
else {
alert(obj.value);
return false
;
}
}
</script>
<body>
<form action=
"http://www.jb51.net" onsubmit=
"return validate(document.getElementByIdx_x(‘myText‘));"
> <!—参数的这种写法注意下-->
<input type=
"text" id=
"myText"
/>
<input type=
"submit" value=
"submit"
/>
</form>
</body>
|
The second way: the onclick= "SubmitForm ()" Will ignore the attributes in other tags, such as the onsubmit attribute in the form tag, by clicking the button to submit the event. In order to validate the form, you can put the validation code in the SubmitForm () and validate it in the method.
?
1234567891011121314151617181920212223 |
<script type=
"text/javascript"
>
function validate() {
if (confirm(
"提交表单?"
)) {
return true
;
}
else {
return false
;
}
}
function submitForm() {
if (validate()) {
document.getElementByIdx_x(
"myForm"
).submit();
}
}
</script>
<body>
<form action=
"http://www.jb51.net" id=
"myForm"
>
<input type=
"text"
/>
<input type=
"button" value=
"submitBtn" onclick=
"submitForm();"
/> <!—也可以使用document.getElementByIdx_x(“该按钮的id”).click();来执行onclick事件-->
</form>
</body>
|
The Third Way: The onsubmit event is placed in the Submit tab, not the form label, when the form validation is invalidated, click the Submit button form to submit directly
?
1234567891011121314151617 |
<script type=
"text/javascript"
>
function validate() {
if (confirm(
"提交表单?"
)) {
return true
;
}
else {
return false
;
}
}
</script>
<body>
<form action=
"http://www.jb51.net"
>
<input type=
"text"
/>
<input type=
"submit" value=
"submit" onsubmit=
"return validate()"
/>
</form>
</body>
|
Fourth: add an onclick event for the submit button, where the event is used for validation of form submissions, similar to the addition of the OnSubmit event in the form label
?
1234567891011121314151617 |
<script type=
"text/javascript"
>
function validate() {
if (confirm(
"提交表单?"
)) {
return true
;
}
else {
return false
;
}
}
</script>
<body>
<form action=
"http://www.jb51.net"
>
<input type=
"text"
/>
<input type=
"submit" value=
"submit" onclick=
"return validate()"
/>
</form>
</body>
|
Fifth way:
?
123456789101112131415161718 |
<body>
<form action=
"http://www.jb51.net" id=
"myForm"
>
<input type=
"text"
/>
<input type=
"button" value=
"submitBtn" id=
"myBtn"
/>
</form>
</body>
<script type=
"text/javascript"
>
function validate() {
if (confirm(
"提交表单?"
)) {
return true
;
}
else {
return false
;
}
}
|
The onclick= "SubmitForm ()" will ignore attributes in other tags, such as the onsubmit attribute in the form tag, by clicking the button to submit the event. In order to verify the form, you can put the validation code in the SubmitForm ();
?
12345678 |
function submitform () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if (validate ()) { document.getelementbyidx_x ( Code class= "JS Plain"). Submit (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; } &NBSP;&NBSP; &NBSP;&NBSP;&NBSP; document.getelementbyidx_x ( </SCRIPT> |
The above this article using JS to submit a form of several methods and verification (must see) is a small part of the whole content to share to everyone, I hope to give you a reference
Several methods and verification of using JS to submit a form (must read)