简体   繁体   English

如何使用数据属性将值从数据库传递到 jquery

[英]How to use data-attribute to pass values from database to jquery

I have an issue passing values from a database to a modal pop up using data attribute.我在使用数据属性将值从数据库传递到模式弹出窗口时遇到问题。

My problem is the data-attribute value passes only one value whereas the edit button is in a php loop and should pick the value for each row.我的问题是数据属性值只传递一个值,而编辑按钮位于 php 循环中,应该为每一行选择值。

Below is a part of my loop code:以下是我的循环代码的一部分:

<td>
  <p data-placement="top" data-toggle="tooltip" title="Edit">
    <a  href="<?php echo admin_url('admin.php?page=woocommerce_checkout&id=' . $query->id.'&status=update'); ?>"  
      class="btn btn-primary btn-xs updatesection"  
      data-title="Edit"
      data-toggle="modal"
      data-id="<?php echo $query->id ?>"
      data-target="#editbilling" 
      data-name="<?php echo $query->name; ?>">Edit</a>
  </p>
</td>

js js

   $(document).on( "click", '#editbilling', function(e) {

     var data = $('.updatesection').data('name');
     alert(data);
   });

So I am testing the values passed through the data attribute by alerting it on javascript.所以我正在测试通过数据属性传递的值,方法是在 javascript 上发出警报。

The problem is only the first id gets submitted for all the edit buttons in a loop carrying different ids instead of submitting different ids for each row in the database.问题是只有第一个id被提交给循环中的所有编辑按钮,带有不同的 id,而不是为数据库中的每一行提交不同的 id。

When I inspect the code on the browser I discover the data-attributes have the corresponding values from the database but they don't get submitted - only the first row gets submitted for all the edit buttons in the loop.当我检查浏览器上的代码时,我发现数据属性具有来自数据库的相应值,但它们没有被提交——只有第一行被提交给循环中的所有编辑按钮。

Can anyone find a fix to this please任何人都可以找到解决此问题的方法吗

I changed the id onclick to class and then used attr instead of data and it worked.我将 id onclick 更改为 class ,然后使用 attr 而不是 data 并且它起作用了。 Please check my solution below:请在下面查看我的解决方案:

  $(document).on( "click", '.updatesection', function(e) {

  var data = $(this).attr('data-id');
   alert(data);
  });  

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

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