【问题标题】:Ajax Call hyperlink on datatable column数据表列上的 Ajax 调用超链接
【发布时间】:2017-03-16 04:20:26
【问题描述】:

我有数据表 json 名称 = table_a.php 我在那个文件上有超链接..

我可以将数据表显示给

<table data-url="table_a.php"></table>

在那一栏有超链接。我想处理那个到 ajax 的链接。

<script>
$(document).ready(function() {
//$(document).on("click", "#stats", function (e) {
    $('#stats').click(function(e) {
        alert("a");
        e.preventDefault();
        $.ajax({
            alert("ab");
            url: 'update_process.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(status) {
                    var datanya = JSON.parse(JSON.stringify(status));
                    $('#table').bootstrapTable('refresh', {url: 'table_a.php'});

            }
        });
        return false;
    });
})
</script>

我尝试点击链接,但从未显示警报。

table_a.php:

<?php
header('content-type:application/json');
include '../koneksi.php';

$select = mysql_query("select * from tb_pemesanan where no_meja='$_GET[meja]'");
$row=array();

while($row=mysql_fetch_array($select))

{
    $select1 = mysql_query("SELECT * FROM tb_pemesanan where id_pemesanan='$row[id_pemesanan]' AND status='Sedang di proses'");
    $status='0';
    if(mysql_num_rows($select1)== 0) {
        $status="<a class='btn btn-success' href='#'><b>Sudah di antar <span class='glyphicon glyphicon-ok'></span></b></a>";
    }else{


    $status="<a id='stats' class='btn btn-primary' href='proses.php'>
    <b>Sedang di proses <span class='glyphicon glyphicon-hourglass'></span></b></a>";
}

$arrayx=array(  "status"=>$status );

$rows[] = $arrayx;

}
echo json_encode($rows);

?>

【问题讨论】:

  • 请出示您的PHP 代码。
  • 我添加了 table_a.php

标签: json ajax datatable


【解决方案1】:

尝试更新您的table_a.php 代码,如下所示。您可以找到一些 cmet 来了解我在程序中所做的更改以及更改的原因。

<?php
  header('content-type:application/json');
  include '../koneksi.php';

  $select = mysql_query("SELECT * FROM tb_pemesanan WHERE no_meja = '$_GET[meja]'");
  $row = array();

  while($row = mysql_fetch_array($select)) {
    $select1 = mysql_query("SELECT * FROM tb_pemesanan WHERE id_pemesanan = '$row[id_pemesanan]' AND status = 'Sedang di proses'");
    $status = '0';
    if(mysql_num_rows($select1) == 0) {
      $status = "<a class='btn btn-success' href='#'><b>Sudah di antar <span class='glyphicon glyphicon-ok'></span></b></a>";
    } else {
      // id = "stats" changed to class="stats"
      $status = "<a class='btn btn-primary stats' href='proses.php'>
        <b>Sedang di proses <span class='glyphicon glyphicon-hourglass'></span></b></a>";
    }

    $arrayx = array("status"=>$status);

    $rows[] = $arrayx;

  }
  echo json_encode($rows);

?>

同时更改您的javascript

<script>
  $(document).ready(function() {
    // $(document).on("click", "#stats", function (e) {
    // jquery counts only first id, so if you want to apply the same for all buttons you should change that id to class name
    $('.stats').click(function(e) {
      alert("a");
      e.preventDefault();
      $.ajax({
        alert("ab");
        url: 'update_process.php',
        data: $(this).serialize(),
        dataType: 'json',
        success: function(status) {
          var datanya = JSON.parse(JSON.stringify(status));
          $('#table').bootstrapTable('refresh', {url: 'table_a.php'});
        }
      });
      return false;
    });
  })
</script>

试试看。如果它仍然没有给出正确的输出,请输入你的 php 脚本输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2019-04-18
    • 2010-09-27
    • 2014-08-14
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多