根据复选框的状态切换文本框的可见性--jquery
所以我有一份调查表的比赛清单。对于那些不符合任何其他标准的人,有一个名为“其他指定”的框。我需要一本教科书在选中“其他指定”时变得可见,并在未选中时消失。我已经能够使该框在选中另一个框时出现,但在未选中时不会消失。任何帮助将不胜感激。谢谢。
脚本
<script type="text/javascript" src="jquery.js">
<script language="JavaScript">
$(function(){
$('#other').click(function(){
$('#otherrace').toggle(
function(event) {
$(event.target).css('visible');
},
function(event) {
$(event.target).css('hidden');
}
);
});
</script>
html
<input type="checkbox" name="race" id="other"value="other" />Other, specify<br />
<div style="visibility:hidden" id="race"><input type="text" name="fname" size="25" maxlength="25" id="otherrace" /></div>
So I have a list of races for a survery form. there is a box named 'other specify' for those people that don't meet any of the other criteria. I need a text book to become visible when 'other specify' is checked, and to dissappear when it's not. I have been able to make the box appear when another is checked, but not dissappear when it was unchecked. Any help would be greatly appreciated. Thanks.
Jscript
<script type="text/javascript" src="jquery.js">
<script language="JavaScript">
$(function(){
$('#other').click(function(){
$('#otherrace').toggle(
function(event) {
$(event.target).css('visible');
},
function(event) {
$(event.target).css('hidden');
}
);
});
</script>
html
<input type="checkbox" name="race" id="other"value="other" />Other, specify<br />
<div style="visibility:hidden" id="race"><input type="text" name="fname" size="25" maxlength="25" id="otherrace" /></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做:
示例: http://jsfiddle.net/patrick_dw/K3kvE/
或使用
display:none
而不是visibility:hidden
并执行以下操作:示例: http://jsfiddle.net/patrick_dw/K3kvE/1/
您使用的方式
.toggle()
是一个“切换事件”,这意味着您有效地分配了一个切换的点击事件在执行您传递给它的函数之间。因为它是被隐藏的
otherrace
的父级,所以您需要使用.parent()
,然后将其visibility
属性设置为hidden
或visible
,具体取决于复选框的状态。第二种解决方案从使用
visibility
属性更改为使用display
,以便toggle()
可以以不同的方式调用。现在调用它的方式,它在show()
和hide()
,设置显示
属性从none
到其原始设置并返回。Do it this way:
Example: http://jsfiddle.net/patrick_dw/K3kvE/
or use
display:none
instead ofvisibility:hidden
and do this:Example: http://jsfiddle.net/patrick_dw/K3kvE/1/
The way you were using
.toggle()
was as a "toggle event", which means you were effectively assigning a click event that toggles between executing the functions you pass it.Because it is the parent of
otherrace
that is being hidden, you need to traverse up to it using.parent()
, then set itsvisibility
property tohidden
orvisible
depending on the state of the checkbox.The second solution changes from using
visibility
property to usingdisplay
so thattoggle()
can be called in a different manner. The way it is being called now, it toggles betweenshow()
andhide()
, which set thedisplay
property fromnone
to its original setting and back.