jQuery和PHP while循环php

时间:2014-12-17 22:41:19

标签: javascript php jquery

我有一个页面可以生成来自while循环的数据,如下所示:

echo "  <ul class=\"task-list\">
    <li>
      <div class=\"task-checkbox\">
          <input type=\"checkbox\" class=\"list-child\" value=\"\"  />
      </div>
      <div class=\"task-title\">
        <div class=\"request_hidden_id\" >$request_id</div>
          <span class=\"task-title-sp\">$request_summary</span>
          <span class=\"badge badge-sm label-success\">2 Days</span>
          <div class=\"pull-right hidden-phone\">
              <button class=\"btn btn-success btn-xs\"><i class=\" fa fa-check\"></i></button>
              <button class=\"btn btn-primary btn-xs\"><i class=\"fa fa-pencil\"></i></button>
              <button class=\"btn btn-danger btn-xs\"><i class=\"fa fa-trash-o \"></i></button>
  </div>
  </div>
</li>
</ul>

我需要引用类(request_hidden_id)值的jQuery如下:

$( ".btn.btn-primary.btn-xs" ).click(function() {
  var acct_id = $('.request_hidden_id').text();
  alert( "are you sure you want to delete " + acct_id );
  $("#edit-service-request-modal").modal('show');
});

当警报触发时,来自while循环的jQuery中给出的类的所有id都显示在警告框中。任何想法如何只参考选定的一个?谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用this引用您点击的元素,然后向上走dom直至到达.task-list容器,然后在容器中搜索当前.request_hidden_id

$( ".btn.btn-primary.btn-xs" ).click(function() {
    var acct_id = $(this).closest('.task-list').find('.request_hidden_id').text();
    alert( "are you sure you want to delete " + acct_id );
    $("#edit-service-request-modal").modal('show');
});

答案 1 :(得分:0)

多个元素将与$('.request_hidden_id')匹配,这就是您获取所有元素的原因。为了使一个在合适的范围内,尝试这样的事情:

$( ".btn.btn-primary.btn-xs" ).click(function() {
    var acct_id = $(this).parents('.task-title').children('.request_hidden_id').text();
    alert( "are you sure you want to delete " + acct_id );
    $("#edit-service-request-modal").modal('show');
});