根据复选框的状态切换文本框的可见性--jquery

发布于 2024-10-06 16:04:18 字数 829 浏览 0 评论 0原文

所以我有一份调查表的比赛清单。对于那些不符合任何其他标准的人,有一个名为“其他指定”的框。我需要一本教科书在选中“其他指定”时变得可见,并在未选中时消失。我已经能够使该框在选中另一个框时出现,但在未选中时不会消失。任何帮助将不胜感激。谢谢。


脚本

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-10-13 16:04:18

这样做:

示例: http://jsfiddle.net/patrick_dw/K3kvE/

$('#other').change(function(){ 
    $('#otherrace').parent().css( 'visibility', this.checked ? 'visible' : 'hidden' );
});

或使用 display:none 而不是 visibility:hidden 并执行以下操作:

示例: http://jsfiddle.net/patrick_dw/K3kvE/1/

$('#other').change(function(){ 
    $('#otherrace').parent().toggle( this.checked );
});

您使用的方式 .toggle() 是一个“切换事件”,这意味着您有效地分配了一个切换的点击事件在执行您传递给它的函数之间。

因为它是被隐藏的 otherrace 的父级,所以您需要使用 .parent(),然后将其 visibility 属性设置为 hiddenvisible,具体取决于复选框的状态。

第二种解决方案从使用 visibility 属性更改为使用 display ,以便 toggle() 可以以不同的方式调用。现在调用它的方式,它在 show()hide(),设置显示属性从 none 到其原始设置并返回。

Do it this way:

Example: http://jsfiddle.net/patrick_dw/K3kvE/

$('#other').change(function(){ 
    $('#otherrace').parent().css( 'visibility', this.checked ? 'visible' : 'hidden' );
});

or use display:none instead of visibility:hidden and do this:

Example: http://jsfiddle.net/patrick_dw/K3kvE/1/

$('#other').change(function(){ 
    $('#otherrace').parent().toggle( this.checked );
});

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 its visibility property to hidden or visible depending on the state of the checkbox.

The second solution changes from using visibility property to using display so that toggle() can be called in a different manner. The way it is being called now, it toggles between show() and hide(), which set the display property from none to its original setting and back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文