简体   繁体   English

如何使用jquery显示隐藏的表单字段

[英]How to use jquery to show hidden form fields

I have three input fields for collecting telephone numbers from users. 我有三个输入字段,用于收集用户的电话号码。 I display one field and hide the other two. 我显示一个字段并隐藏另外两个字段。 I have placed a link(Add home number) below it and when you user clicks on the link it shows the hidden input field. 我在其下方放置了一个链接(添加家庭电话),当用户单击链接时,它会显示隐藏的输入字段。 And I have placed one more link below it when clicked displays the last input field. 当点击显示最后一个输入字段时,我在它下面再添加了一个链接。

  <input type="text" />
  <a href="#" class="show" >Add Home Number</a>
  <div style="display: none">
         <input type="text" />
  <a href="#" class="show" >Add Office Number</a>
  </div>               
  <div style="display: none">
         <input type="text" />
  </div>               

And the jquery looks like this.. jquery看起来像这样..

<script>
    $(function(){ 
    $(".show").click(function () {
    $(this).next("div").show("slow");
    });
    });
</script>

The first link works fine, But the second one does not work. 第一个链接可以正常工作,但是第二个链接不能工作。

I appreciate all help. 我感谢所有帮助。

Thanks. 谢谢。

you have to use the each function: 你必须使用每个功能:

like this: 像这样:

$(".show").each($(this).click(function () {
    $(this).next("div").show("slow");
    })
);

.next() only selects the next sibling element. .next()仅选择下一个兄弟元素。 You links have no subsequent siblings, because you close the parent element (the div) immediately after. 您的链接没有后续的兄弟,因为您之后立即关闭父元素(div)。 You need to select the next sibling of the div: 您需要选择div的下一个兄弟:

<script>
    $(function(){ 
        $(".show").click(function () {
            $(this).parent().next("div").show("slow");
        });
    });
</script>

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

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