简体   繁体   English

在提交表单之前,如何检查HTML中的表单元素?

[英]How do I check form elements in HTML before the form has been submitted?

I'm using PHP to validate and run my form, however, I want to check elements as they are filled out. 我正在使用PHP验证并运行表单,但是,我想检查元素是否已填写。 How do I check an element for its current value prior to the submission of the form? 在提交表单之前,如何检查元素的当前值?

You need to use Javascript to validate the data in the form without sending it to the server. 您需要使用Javascript来验证表单中的数据,而无需将其发送到服务器。

http://www.elated.com/articles/form-validation-with-javascript/ may be useful http://www.elated.com/articles/form-validation-with-javascript/可能会有用

Documents have a forms collection, forms have an elements collection, elements that are form controls have associated DOM properties such as value , checked , selected and so specified in their related interfaces (eg HTMLInputElement , HTMLSelectElement ) that can be compared to allowed values and ranges. 文档具有表单集合,表单具有元素集合,表单控件的元素具有关联的DOM属性,例如valuecheckedselected和在其相关接口(例如HTMLInputElementHTMLSelectElement )中指定的属性 ,可以将其与允许的值和范围进行比较。

There are many, many questions and articles on the subject. 关于这个主题有很多很多问题和文章。 Search for form validation, have a go, ask questions. 搜索表单验证,开始尝试,提出问题。

You have two choices: either validat it with javascript before the form is submitted, or you check by PHP on the page it has been submitted to. 您有两种选择:要么在提交表单之前使用javascript对其进行验证,要么在提交表单的页面上通过PHP进行检查。 usually it is wise to do both, in case javascript is turned off. 通常,在关闭javascript的情况下,最好同时执行这两项操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM