DataTables和jQuery在表中执行onclick事件

时间:2017-09-04 20:13:32

标签: javascript jquery datatables

我想通过单击表格的行来执行jquery onclick事件。问题是"发现" jquery函数不适用于数据表。

<table id="GVCausal" class="table table-hover table-striped text-nowrap">
    <thead>
        <tr>
            <th class="uno">@Html.CheckBox("chkAll")</th>
            <th class="uno">Código</th>
            <th>Nombre</th>
            <th class="hide">&nbsp;</th>
            <th class="uno">Indemn.</th>
            <th class="uno">Desahuc.</th>
        </tr>
    </thead>
    <tbody>
        <tr role="row" class="odd">
            <td>
                <input data-val="true" data-val-number="The field referencia must be a number." data-val-required="The referencia field is required." id="referencia" name="[0].referencia" type="hidden" value="14">
                <input id="chkDel" name="chkDel" type="checkbox" value="true"><input name="chkDel" type="hidden" value="false">
            </td>
            <td>
                <input data-val="true" data-val-number="The field id must be a number." data-val-required="The id field is required." name="[0].id" type="text" value="14">
            </td>
            <td>
                <input name="[0].nombre" type="text" value="NECESIDADES DE LA EMPRESA ,ESTA">
            </td>
            <td class="hide">Detalle de causal</td>
            <td>
                <input name="[0].indemnizac" type="text" value="S">
            </td>
            <td>
                <input name="[0].desahucio" type="text" value="S">
            </td>
        </tr>
    </tbody>
</table>


<input id="idcausal" name="idcausal" type="hidden" value="">
<textarea class="form-control" cols="20" id="detallecausal" name="detalle" onkeyup="InputChanged(this)" rows="15"></textarea>

这是我的尝试,但它对数据表不起作用(例如使用分页)

<script>
    $(document).ready(function () {
        var tabla = $('#GVCausal').DataTable();
        $('#GVCausal tbody').on('click', 'tr', function () {
            var fila = tabla.row(this).data();
            $('#detallecausal').val(fila[3]);
            $('#idcausal').val(fila.find('input[name*=id]').val());
        });
    });
</script>

1 个答案:

答案 0 :(得分:1)

请改用以下代码:

$('#GVCausal tbody').on('click', 'tr', function () {
    var fila = tabla.row(this).data();
    $('#detallecausal').val(fila[3]);
    $('#idcausal').val($('input[name*=id]', this).val());
});