简体   繁体   English

两种“形式”合而为一 <form> 冲突其必需的属性

[英]Two “forms” in one <form> clash their required attributes

The issue : I have a single page with two email forms (one <form> , two divs inside this which look like forms to the user). 问题 :我有一个页面,其中包含两个电子邮件表单(一个<form> ,其中两个div对用户来说看起来像表单)。 When you submit either form, the C# behind looks at the values of the form and sends them in an email. 当您提交任何一个表单时,后面的C#都会查看该表单的值并将其通过电子邮件发送。 This works well, and now I want a little validation, but I can't add required attributes to the inputs as the code doesn't know it's two separate "forms" and needs to handle the required attributes separately for each one. 这很好用,现在我需要进行一些验证,但是我无法将required属性添加到inputs因为代码不知道这是两个单独的“表单”,并且需要分别处理每个表单的required属性。

I understand why this issue occurs, but I want to know if there's a way to tell the page to handle the required attributes in groups. 我知道为什么会发生此问题,但是我想知道是否有一种方法可以告诉页面以组的形式处理required属性。

What I've tried : Both "forms" are handled by the code behind, and so need to be run server-side, so separate <form> elements wouldn't work. 我已经尝试过 :两个“表单”都由后面的代码处理,因此需要在服务器端运行,因此单独的<form>元素将不起作用。 I have tried nesting the "forms" as <form> elements inside the server-side <form> , which separates the required attributes as desired, but seemingly breaks a number of things and I've read this is generally bad practice anyway. 我曾尝试将“表单”作为<form>元素嵌套在服务器端<form> ,该元素根据required分隔required属性,但看似破坏了很多东西,并且我读到这通常是一种不好的做法。

What I'm not asking : To validate the form in the code behind. 我不是要问的 :要验证后面代码中的表单。

Edit : David's answer works great to validate groups of textboxes, which is what I needed. 编辑 :David的答案非常适合验证文本框组,这是我所需要的。

To additionally validate a checkbox, I used javascript. 为了进一步验证复选框,我使用了javascript。 onclientclick we can check if the checkbox is checked AND validate using Page_ClientValidate('validationgroup') and then return true, else return false. onclientclick我们可以检查checkbox是否已选中,并使用Page_ClientValidate('validationgroup') ,然后返回true,否则返回false。 onclick is only fired when onclientclick returns true. 仅当onclientclick返回true时才触发onclick

use one form with two submit buttons. 使用带有两个提交按钮的一种形式。 Use validationgroup to distinguish 使用validationgroup来区分

http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx

如果您为每个提交按钮编写自己的验证功能,则应该可以

<input type='submit' onclick='return validateFirstButton()'/>

To validate checkboxes follow the instructions here: 要验证复选框,请按照此处的说明进行操作:

http://codeclimber.net.nz/archive/2007/07/19/How-to-add-a-required-validator-to-a-CheckBoxList.aspx http://codeclimber.net.nz/archive/2007/07/19/How-to-add-a-required-validator-to-a-CheckBoxList.aspx

Because this derives from BaseValidator you will be able to use groups. 因为这是从BaseValidator派生的, BaseValidator您将能够使用组。 Hope this helps 希望这可以帮助

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

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