如何使用ajax脚本在td内附加模式按钮

发布于 2025-01-11 08:38:48 字数 1802 浏览 0 评论 0原文

我在 html 表中插入模式按钮时遇到问题。我正在使用 AJAX 和附加内置函数。我尝试了这种方法

“开放模式”,

但它不起作用。我尝试删除“class”、“type”等按钮属性,它有效,但我需要这些。对此有任何方法。谢谢。

这是我的代码

<script type=text/javascript>
      $(document).ready(function() {
       });
    
       function getData(id){
      $('#myModal').modal('show');
          $.ajax({ 
          type: "GET",
          url: "getproducts/",
          dataType: 'json',
           data: {id: id}, 
          success: function (response) {
            var len = 0;
            if(response['data'] !=null){
              len = response['data'].length;  
            }
           
            $('#userTable tbody').empty();
            if(len > 0){
              for(i = 0; i < len; i++) {
                var p_id = response['data'][i].p_id;
                var p_name = response['data'][i].p_name;
                var p_amount = response['data'][i].p_amount;
                var m_fname = response['data'][i].m_fname;


                var tr_str = 
                "<tr>" +
                   "<td>" + p_id + "</td>"+
                   "<td>" + p_name + "</td>" +

                  //  HOW CAN I ADD MODAL BUTTON HERE THIS APPROACH DOESN'T WORKED
                   "<td> <button type="button" class="btn btn-primary" data-bs-toggle="modal" 
                     data-bs-target="#myModal2">Open modal</button></td>"                         
               
                "<tr>";

                $('#userTable tbody').append(tr_str); 
              }
            }
      
       }
       });
    };
    
  </script>

预期输出应该是这样的

预期输出

I have problem inserting modal button inside my html table. I'm using AJAX and append built in function. I tried this approach

" Open modal"

but it did'nt work. I tried to remove the button properties like 'class', 'type' , etc and it worked but I need those. Any approach for this. Thank you.

This is my code

<script type=text/javascript>
      $(document).ready(function() {
       });
    
       function getData(id){
      $('#myModal').modal('show');
          $.ajax({ 
          type: "GET",
          url: "getproducts/",
          dataType: 'json',
           data: {id: id}, 
          success: function (response) {
            var len = 0;
            if(response['data'] !=null){
              len = response['data'].length;  
            }
           
            $('#userTable tbody').empty();
            if(len > 0){
              for(i = 0; i < len; i++) {
                var p_id = response['data'][i].p_id;
                var p_name = response['data'][i].p_name;
                var p_amount = response['data'][i].p_amount;
                var m_fname = response['data'][i].m_fname;


                var tr_str = 
                "<tr>" +
                   "<td>" + p_id + "</td>"+
                   "<td>" + p_name + "</td>" +

                  //  HOW CAN I ADD MODAL BUTTON HERE THIS APPROACH DOESN'T WORKED
                   "<td> <button type="button" class="btn btn-primary" data-bs-toggle="modal" 
                     data-bs-target="#myModal2">Open modal</button></td>"                         
               
                "<tr>";

                $('#userTable tbody').append(tr_str); 
              }
            }
      
       }
       });
    };
    
  </script>

Expected output should be like this

expected output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2025-01-18 08:38:48

我认为字符串连接的问题
我给出一个代码。请更换它。

"<td> <button type='button' class='btn btn-primary' data-bs-toggle='modal' 
                     data-bs-target='#myModal2'>Open modal</button></td>" 

I think the problem with string concatenation
I am giving a code. please replace it.

"<td> <button type='button' class='btn btn-primary' data-bs-toggle='modal' 
                     data-bs-target='#myModal2'>Open modal</button></td>" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文