提交没有Javascript的表单

提交没有Javascript的表单

本文介绍了提交没有Javascript的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单依赖于JavaScript来完成所有客户端表单验证。如果所有字段都填写正确,而不是使用标准的提交按钮,则表单通过JavaScript提交:

 << ; button name =buttontype = button onclick =validateReqFields('mForm','username,password,compPassword,firstName,lastName');> Register< / button> 

document [formName] .submit();

如果客户端禁用了javascript,所有表单验证都会在服务器端完成(它实际上会再次执行无论如何,这并不重要)。问题在于使用按钮而不是提交按钮。它适用于JavaScript,但如何解决这个问题,当JavaScript不可用?如果我使用提交按钮和JavaScript一起提交表单,每次按下按钮都将无法正常工作。 解决方案

使用提交按钮而不是按钮按钮,然后让您的 validateReqFields 函数在表单无效时返回false。



然后从validateReqFields中提取表单,如果你愿意的话。



这样


  • 如果表单有效,那么按钮点击会起泡并且表单将被提交

  • 如果表单无效,那么javascript将取消按钮单击

  • 如果javascript被禁用,它将作为后备提交给服务器


I have a form that relies on javascript to do all the client side form validation. The form is submitted via javascript if all the fields are correctly filled in as opposed to using a standard "submit" button:

<button name="button" type=button onclick="validateReqFields('mForm', 'username, password, compPassword, firstName, lastName');">Register</button>

document[formName].submit();

If the client has javascript disabled, all of the form validation is done server side (its actually performed again regardless but that doesn't really matter). The problem lies with using a button with a type of button instead of submit. It works perfect with javascript, but how do I get around this when javascript is not available? If I use a submit button along with the javascript then it submits the form with each button press and doesn't work properly.

解决方案

Use a submit button instead of the "button" button, and then have your validateReqFields function return false if the form is not valid.

Then take out the form submit from the validateReqFields if you like.

This way

  • If the form is valid, then the button click will bubble and the form will submit
  • If the form is invalid, then the javascript will cancel the button click
  • If javascript is disabled, then it will be submitted to the server as a fallback

这篇关于提交没有Javascript的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:16