weixin_33676492 2015-10-06 14:35 采纳率: 0%
浏览 25

DataTable-Ajax源数据

I have a table and filled it with ajax

TABLE

<table id="tabla_productos"> </table>

Ajax sourced data

var tabla_productos = $('#tabla_productos').DataTable( {
"responsive" : "true",
"ajax": {
    "url": "/funciones/getProductos.php",
    "dataSrc": ""
},
"columns": [
    { "data": "id" },
    { "data": "add_cart" }
]
}}); 

AJAX DATA

{"id":"2",
 "add_cart":"<form class=\"form-item\"><input type=\"hidden\" value=\"1\" name=\"product_qty\"><input type=\"hidden\" value=\"S10881\" name=\"product_code\"><button class=\"btn\" type=\"submit\">Add to Cart<\/button><\/form>" }

jQuery Form submit:

$(".form-item").submit(function(e){
    alert('ok');        
    e.preventDefault();
});

HTML OUTPUT: form created from ajax

<form class="form-item"><input type="hidden" name="product_qty" value="1"><input type="hidden" name="product_code" value="S10881"><button type="submit" class="btn">Add to Cart</button></form>

SAME FORM CREATED FROM HTML:

<form class="form-item"><input type="hidden" name="product_qty" value="1"><input type="hidden" name="product_code" value="S10881"><button type="submit" class="btn">Add to Cart</button></form>
  • When i submit the form created from html it works ok.
  • If i submit the form created from ajax it doesn't works.
  • If i wait that table fill up with data, it works:

    setTimeout(function(){ $(".form-item").submit(function(e){ alert('ok');
    e.preventDefault(); }); }, 10000);

  • 写回答

1条回答 默认 最新

  • weixin_33697898 2015-10-06 17:15
    关注

    i added "initComplete" with form submit function to data table

        var tabla_productos = $('#tabla_productos').DataTable( {
    "responsive" : "true",
    "ajax": {
        "url": "/funciones/getProductos.php",
        "dataSrc": ""
    },
    "columns": [
        { "data": "id" },
        { "data": "add_cart" }
    ],
    "initComplete": function(settings, json) {
      $(".form-item").submit(function(e){
       alert('ok');        
       e.preventDefault();
      });
    }
            }}); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图
  • ¥15 智能除草机器人方案设计
  • ¥15 对接wps协作接口实现消息发送
  • ¥15 SQLite 出现“Database is locked” 如何解决?