[英]How do you ignore hidden page elements when validating using javascript
basically I have attending yes or no. 基本上我参加了是或不是。 if 'no' is selected then the drop down menus are hidden. 如果选择“否”,则隐藏下拉菜单。
but when clicked submit a validation error still occurs. 但是单击提交后仍然会发生验证错误。 Is there a way to ignore hidden elements when validating ? 验证时是否可以忽略隐藏的元素?
my page script 我的页面脚本
1. <SCRIPT LANGUAGE="JavaScript"'>
2. <!--
3. function validateForm(){
4. if(document.colour.list1.selectedIndex==0)
5. {
6. alert("Please select an Item.");
7. document.colour.list1.focus();
8. return false;
9. }
10. return true;
11. }
12. //-->
13.
14.
15.</script>
16.<script type="text/javascript">
17.window.onload=registerEventHandlers;
18. function registerEventHandlers()
19.{
20. document.getElementById("radio1").onclick = function(){
21. hideDiv(this,"list1")
22. };
23. document.getElementById("radio2").onclick = function(){
24. showDiv(this,"list1")
25. };
26. }
27.
28. function showDiv(targetElement,toggleElementId){
29.
30. var showAll=document.getElementsByTagName("div"),
31. i,
32. re = new RegExp('\\b' + toggleElementId + '\\b');
33. for(i=0; i < showAll.length; i++){
34. if (re.test(showAll[i].className)) {
35. showAll[i].style.visibility="visible";
36. showAll[i].style.display="block";
37. }
38. }
39.}
40.function hideDiv(targetElement,toggleElementId){
41. var hideAll=document.getElementsByTagName("div"),
42. i,
43. re = new RegExp('\\b' + toggleElementId + '\\b');
44. for(i=0; i < hideAll.length; i++){
45. if (re.test(hideAll[i].className)) {
46. hideAll[i].style.visibility="hidden";
47. hideAll[i].style.display="none";
48. }
49. }
50.}
51.</script>
52.
53.
54.
55.Yes:<input type="radio" id="radio2" name="yesNo" value="yes" />
56.No:<input type="radio" id="radio1" name="yesNo" value="no"/>
57.<div class="list1" style="display: none;" >
58. <select name="colour">
59. <option>Please Select</option>
60. <option>red</option>
61. <option>orange</option>
62. <option>blue</option>
63. </select>
64.</div>
65. <div class="list1" style="display: none;" >
66.<select name="shade">
67. <option>Please Select</option>
68. <option>dark</option>
69. <option>light</option>
70.</select>
71.</div>
You just have to change your javascript code to check whether those items are hidden and skip the validation code if they are hidden. 您只需要更改JavaScript代码即可检查这些项目是否被隐藏,如果它们被隐藏,则跳过验证代码。 You'd have to show us how they were hidden and what your HTML is for us to offer the simplest way to detect if they are hidden. 您必须向我们展示它们是如何隐藏的,以及您的HTML是什么,以便我们提供检测它们是否隐藏的最简单方法。
PS You should know that hidden items will still be sent with a form submission so you may need to make sure your server knows which data is expected to be valid and which is not (because it was hidden). PS您应该知道,隐藏的项目仍将与表单提交一起发送,因此您可能需要确保服务器知道哪些数据应该有效,哪些无效(因为它是隐藏的)。
If you the option of using jQuery, I would suggest you do. 如果您选择使用jQuery,我建议您这样做。 Then it's as simple as doing 然后就像做一样简单
if($(element).is(":visible")){
}
Of course you need to replace "element" with the element you're testing for validation. 当然,您需要用要测试的元素来替换“ element”以进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.