简体   繁体   English

如何使用Internet Explorer中的复选框隐藏和取消隐藏Javascript和CSS

[英]How to hide & unhide with Javascript & CSS using a checkbox in Internet Explorer

I tried for days to get this to work in IE, and ultimately gave up by presenting the user with an alert window warning users that the form does not fully work in IE. 我花了几天时间尝试使它在IE中工作,最终放弃了向用户显示一个警告窗口的警告窗口,警告用户该表单无法在IE中完全工作。 Yet, people are still using IE to fill out the form and I'm having to email back and forth with them to finish their registration, so I'd like to see if I can actually fix the original problem. 但是,人们仍在使用IE填写表单,我不得不与他们来回发送电子邮件以完成注册,所以我想看看我是否真的可以解决原始问题。 I have a long registration form for an event here: 我在这里有一份活动的报名表格:

http://rhythmshuffle.com/RS2013/Register.html http://rhythmshuffle.com/RS2013/Register.html

I decided to handle the length of the form by revealing form elements when the user clicks various checkboxes, eg the housing section at the very bottom of the form. 我决定通过在用户单击各种复选框时显示表单元素来处理表单的长度,例如,表单底部的外壳部分。 Here's the javascript: 这是JavaScript:

function togglehide(thiselem) {
  var item = document.getElementById(thiselem);
  if (item) {if (item.className == 'unhidden') {item.className='hidden';} else {item.className='unhidden';}}
}

here's the relevant CSS: 这是相关的CSS:

.hidden {display: none; overflow: hidden; border: none; text-decoration: none; background: transparent;}
.unhidden {display: block; overflow: auto; border: none; text-decoration: none; background: transparent;}

and here's an example of the html: 这是html的示例:

<input type="checkbox" name="housing" value="yes" onClick="javascript:togglehide('housingrequest')"/>
<div id=housingrequest class=hidden>
  whatever - more form elems...
</div>

Does anyone know how to make this work in IE? 有谁知道如何在IE中进行这项工作? I searched Stack Overflow awhile back when I initially worked on this. 最初从事此工作时,我搜索了一段时间的Stack Overflow。 I'd tried a number suggestions that were answers to similar but slightly different problems, but none of them had worked for me (I'd tell you what I tried, but cannot remember now). 我尝试了一些建议,虽然这些建议可以解决相似但略有不同的问题,但是没有一个对我有用(我会告诉您我尝试了什么,但现在不记得了)。

Thanks, Rob 谢谢,罗伯

UPDATE: I confirmed myself that the code WORKS in IE 9 & 10 (despite what other testers were telling me). 更新:我确认自己该代码在IE 9和10中有效(尽管其他测试人员告诉我)。 So this question is specifically for IE 8 and below. 因此,此问题专门针对IE 8及更低版本。 Apparently, some of my users are still using outdated versions of IE. 显然,我的某些用户仍在使用IE的过时版本。 In fact, one preferred printing out the screen shots I sent her to mail me the registration instead of installing any other up-to-date browser! 实际上,我更喜欢打印出我发送给她的屏幕快照,以邮寄给我注册信息,而不是安装其他任何最新的浏览器! sigh

first give a id to input: 首先输入一个ID:

<input id="test" type="checkbox" name="housing" value="yes"/>
<div id="housingrequest" class="hidden">
  whatever - more form elems...
</div>

then use this jquery code: 然后使用以下jQuery代码:

$("input#test").click(function(){
    if($(this).is(":checked")){
        $("div#housingrequest").show();
    }
    else{
        $("div#housingrequest").hide();
    }
});

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

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