防止在多个下拉列表中选择相同的值
我有四个具有相同项目/值的下拉列表。我想防止在上传表单时多次选择相同的值。我如何使用自定义验证器来做到这一点?谢谢!
I have four dropdown lists with same items/values. I want to prevent the same value is chosen more than once when the form is uploaded. How can I do that using a custom validator? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我主张 Kyra 的第一个解决方案是修改下拉列表,以便无法选择相同的值,预防问题总是比告诉用户他们做错了什么要好。但是,如果您确实想使用 CustomValidator 下面的代码可以工作:
然后在后面的代码或脚本标记中。
显然,这可以进一步细化,如果需要,可以使用 JavaScript 函数来提供客户端验证,使用 ClientValidationFunction 属性。
I would advocate Kyra's first solution to modify the dropdown lists so that the same value cannot be selected, it is always better to prevent problems than tell the user what they did was wrong. However, if you do want to use a CustomValidator the following code would work:
And then in the code behind, or a script tag.
This can obviously be further refined, and a javascript function can be used to provide client side validation if desired using the ClientValidationFunction property.
这可能不是最好的答案,但您始终可以向每个下拉列表添加一个动作侦听器,以便每当其中一个下拉列表的选择发生更改时都会调用它。
这样,当第二个下拉列表中的选定元素发生更改时,它会调用其动作侦听器,然后在其中重置其他下拉列表,以便它们不显示所选值,或者如果可能的话进行设置,以便您无法选择该值
或
当其中一个下拉列表的选择发生变化时,您检查它是否等于其他下拉列表中的其他选定值,如果是,则向用户显示一个消息框和/或将选择更改为空白选择或第一个尚未使用的可用值。
This might not be the best answer but you can always add an actionlistener to each dropdown list so that it is called whenever the selection changes for on of the dropdown lists.
This way when the selected element in the second dropdown list changes it calls its actionlistener and then within there you reset the other dropdown lists so that they don't show that selected value or if possible set it so you can't select that value
OR
When the selection changes for one of the dropdown lists you check to see if it equals on of the other selected values in the other dropdown lists and if it does you either have a msgbox displayed to the user and/or change the selection to a blank selection or the first available value that isn't already used.
只需对 hasSameValue 函数做一些工作即可。这似乎对我有用。感谢您的帮助。
}
Just a bit work on the haveSameValue function. This seems to work for me. Thanks for your help.
}