如何使用jQuery Data表使用onChange事件获取所选数据?

时间:2017-01-14 08:21:22

标签: javascript php jquery ajax

我正在研究jQuery Data表,从mysql数据库加载一些数据。

以下是html代码:

<table id="employee-grid"  cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
    <thead>
        <tr>
            <th>#</th>
            <th>User Id</th>
            <th>Address</th>
            <th>Package Name</th>
            <th>Status</th>
            <th>Action</th>
        </tr>
    </thead>
</table>

jQuery数据表和我的Ajax请求的jQuery代码:

$(document).ready(function() {

    var dataTable = $('#employee-grid').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
            url :"server_processing.php", // json datasource
            type: "post",  // method  , by default get
            error: function(){  // error handling
                $(".employee-grid-error").html("");
                $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                $("#employee-grid_processing").css("display","none");

            }
        }
    } );

    $("#employee-grid").on('change', function() {
        var status = $('.changeStatus').val();
        alert(status);
        $.ajax({
            url : "<?php echo SITE_URL . 'toplevel/update-data'; ?>",
            data : {
                'statusChange' : status
            }, 
            type : 'POST',
        });
    });

});

但当我选择该选项时,每次传递第一个选项值

例如此selected选项标记具有以下值:

<option value='1|11' $statusMsg1>Active</option>
<option value='2|11' $statusMsg2>Blocked</option>

总是传递1|11值!!它应该通过我选择的选项值。我不明白为什么会发生这种情况:(

1 个答案:

答案 0 :(得分:1)

更改

$(".changeStatus").on("change", function(){
    var status = $('.changeStatus').val();
    alert(status);

    // e.t.c...
});

this

$(".changeStatus option").change(function(){
    var status = $(this).val();

    $.ajax({
        url: "<?= SITE_URL . 'toplevel/update-data'; ?>",
        method: "POST",
        data: { statusChange : status }, 
        success: function(data){
            alert(status);
        }
    });
});

希望它有所帮助!