dtmbc1606 2010-07-29 12:40
浏览 70
已采纳

使用Jquery多个值?

I'm trying to display a reply form for each comment but I hide the form using Jquery and I was wondering how can I grab the comment id and add it to Jquery so it can hide each reply form and display the correct reply form correctly instead of displaying the wrong reply form?

For example, form_show_hide1, form_show_hide2, form_show_hide3, form_show_hide4 and so on.

I;m using PHP and JQuery.

Here is the Jquery code.

  $(function(){
      $('a#form_show_hide').click(function(){
        $('#form_container').slideToggle('slow');
        // prevent default action
        return false;
      });
    });

Here is the HTML code. Note I changed the ids to classes.

<a href="#" title="Reply Link" class="form_show_hide">Reply</a>


<form method="post" action="' .htmlentities($_SERVER['REQUEST_URI']) . '" class="form_container>
    <fieldset>
        <textarea rows="6" cols="70"></textarea>
        <input type="submit" name="reply" value="Submit" />
        <input type="hidden"  name="id" value="' . urlencode($comment_id) . '"  />
    </fieldset>
</form>
  • 写回答

2条回答 默认 最新

  • dphw5101 2010-07-29 12:51
    关注

    You can do it by finding the .form_container element relatively using .next() like this:

    $(function(){
      $('a.form_show_hide').click(function(){
        $(this).next('.form_container').slideToggle('slow');
        return false;
      });
    });
    

    There's spacing in your code which gives me doubts though. If they're siblings but not immediate siblings use .nextAll('.form_container:first') rather than the .next() call.

    If they're not siblings but are in some sort of parent container you can identify, for example:

    <div class="container">
      <div>
        <a href="#" title="Reply Link" class="form_show_hide">Reply</a>
      </div>
      <form method="post" action="' .htmlentities($_SERVER['REQUEST_URI']) . '" class="form_container>
        <fieldset>
          <textarea rows="6" cols="70"></textarea>
          <input type="submit" name="reply" value="Submit" />
          <input type="hidden"  name="id" value="' . urlencode($comment_id) . '"  />
        </fieldset>
      </form>
    </div>
    

    Then go up to that container and back down, replacing the .next() call with .closest('.container').find('.form_container'). .closest() just needs a selector to identify that parent container correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题