简体   繁体   English

jQuery和PHP while循环php

[英]jQuery and PHP while loop php

I have aa page that produces data from a while loop as follows: 我有一个页面,它从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>

A jQuery that I need to reference the class( request_hidden_id ) value is as follows: 我需要引用class( 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');
});

When the alert fires, all the id with the class given in the jQuery from the while loop shows in the alert box. 警报触发时,while循环中jQuery中给定类的所有ID都会显示在警报框中。 Any ideas how to have it only reference the one selected? 有什么想法只能参考所选的吗? Thank you 谢谢

You can use this to reference the element you clicked, then walk up the dom till you reach the .task-list container, then search the container for the current .request_hidden_id 您可以使用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');
});

Multiple elements will match $('.request_hidden_id') , that's why you're getting all of them. 多个元素将匹配$('.request_hidden_id') ,这就是为什么要获取所有元素的原因。 In order to get the one in the right scope, try something like this: 为了使它在正确的范围内,请尝试如下操作:

$( ".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');
});

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

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