简体   繁体   English

单击链接时如何使表单呈现?

[英]How can I make a form render when a link is clicked?

I'm newbie in javascript and I want to render a form when user clicks a button, I tried different methods but I wasn't successful. 我是javascript的新手,我想在用户单击按钮时呈现表单,我尝试了其他方法,但未成功。 Here is the code: 这是代码:

 <a href="" id="reply">Reply</a>
 <form action="" method="post" >
     <textarea name="comment" class="comment_textarea"></textarea>
     <input type="hidden" name="id" value="5"> 
     <input type="submit" class="btn"  value="Reply">
 </form>

I'm using this in threaded comments. 我在线程注释中使用它。 I just want to render this form when user clicks reply button and otherwise this form should not be displayed. 我只想在用户单击“答复”按钮时呈现此表单,否则不应显示此表单。

You can hide form using css: 您可以使用CSS隐藏表单:

 <a hred="" id="reply">Reply</a>
 <form action="" method="post" id="reply-form" style="display:none">
     <textarea name="comment" class="comment_textarea"></textarea>
     <input type="hidden" name="id" value="5"> 
     <input type="submit" class="btn"  value="Reply">
 </form>

and then show it when user clicks reply button: 然后在用户单击回复按钮时显示它:

$('#reply').click(function() { $('#reply-form').toggle(); });

UPDATE Now as I saw your HTML: 更新现在,当我看到您的HTML时:

Firstly, never use one id for more than one element, use class instead: 首先,请勿将一个ID用作多个元素,而应使用class:

<a hred="" class="reply">Reply</a>

Now you can use next() to toggle form: 现在,您可以使用next()来切换表单:

//  for each element with class reply, find next form element and toggle visibility
$('.reply').click(function() { $(this).next('form').toggle(); });​​​​

As @des said, you could use a#reply as a trigger. 作为@des说,你可以使用a#reply作为触发。 In jquery it only needs a couple of lines of code: 在jquery中,它只需要几行代码:

  $('.reply').click(function() { 

  $(this).parent().find('.reply-form').toggle(); 


 });​

Example

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

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