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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法