【问题标题】:DataTables and jQuery to Perform onclick event in a TableDataTables 和 jQuery 在表中执行 onclick 事件
【发布时间】:2017-09-04 20:13:31
【问题描述】:

我想通过单击表格的行来执行 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>

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    改用下面的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多