验证页面的多个部分,但只有一个提交按钮
我正在编写一个页面,该页面有一个表单标签和一个提交按钮,但有多个输入点......
<form>
...
</table class="t1">
<tr>
<td><input name="name1" /></td>
<td><input name="stDate" /></td>
<td><input name="endDate" /></td>
<td><input name="description1" /></td>
</tr>
</table>
<table class="t2">
<tr>
<td><input name="name2" /></td>
<td><input name="date2" /></td>
<td><input name="description2" /></td>
<td><input name="email2" /></td>
<td><input name="id2" /></td>
</tr>
</table>
<table class="t3">
<tr>
<td><input name="name3" /></td>
<td><input name="date3" /></td>
<td><input name="description3" /></td>
</tr>
</table>
....
<div align="center" class="submit">
<input class="submit" width="10%" type="submit" name="submit" id="submit" value="Submit" />
</div>
</form>
这些字段都不是必需的,用户可以填写每个字段,也可以不必填写任何字段字段。我遇到的问题是,如果用户填写表“t1”上的任何字段,那么我需要要求表“t1”也填写其他字段......并且相同对于表“t2”和“t3”。 --另外,请注意,输入字段是唯一的,因为我在提交后写入数据库。感谢您的帮助,如果需要,请索取更多信息。
I am writing a page that has one form tag and one submit button, but has multiple spots for input....
<form>
...
</table class="t1">
<tr>
<td><input name="name1" /></td>
<td><input name="stDate" /></td>
<td><input name="endDate" /></td>
<td><input name="description1" /></td>
</tr>
</table>
<table class="t2">
<tr>
<td><input name="name2" /></td>
<td><input name="date2" /></td>
<td><input name="description2" /></td>
<td><input name="email2" /></td>
<td><input name="id2" /></td>
</tr>
</table>
<table class="t3">
<tr>
<td><input name="name3" /></td>
<td><input name="date3" /></td>
<td><input name="description3" /></td>
</tr>
</table>
....
<div align="center" class="submit">
<input class="submit" width="10%" type="submit" name="submit" id="submit" value="Submit" />
</div>
</form>
None of these fields are required, the user can fill out every field or doesn't have to fill out any of the fields. Where I run into the problem is, if the user fills out the any of the fields on table "t1" then I need to require that the other fields be filled out as well for table "t1".....and the same for table "t2" and "t3". --Also, note, the input fields are unique, bc I am writing to a DB after submit. Thanks for the help, and please request more info it if is needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做这样的事情:
无论如何,类似的事情
You could do something like:
Something like that anyway
您可以做的一件事是使用 getElementsByName。然后你将得到名称、日期和描述的数组。然后您可以迭代它们并确保如果名称[0]不为空,则日期[0]和描述[0]不为空等。
更新以支持更新的问题:
在这种情况下更好的选择是使用 jquery,它提供了更简单的语法来执行相同的操作。您可以使用一个虚拟 css 类来用于形成 FORM 部分的表。例如
“formsection”是虚拟的CSS类。
然后使用 jquery 你可以获得 $(".formsection") 它将给出所有表单部分。 JQuery 允许您获取给定元素的特定元素类型的子元素。使用它,您可以获得每个表单部分的输入元素。
完成后,您可以循环浏览这些输入元素,看看是否全部为空白或全部填充。
One thing you can do is use getElementsByName. Then you will get arrays of name, date and description. Then you can iterate through them and make sure that if name[0] is not blank then date[0] and description[0] are not blank etc.
Update to support the updated question:
In that case a better option would be to use jquery which provides easier syntax for doing the same. You can have a dummy css class to use for your tables that form the FORM sections. e.g.
"formsection" is the dummy css class.
Then using jquery you can get $(".formsection") which will give all the form sections. JQuery allows you to get the children of a specific element type of a given element. Using that you can get the input elements for a each of the form section.
Once you have that you can loop through those input elements to see if all are blank or all arte filled.