javascript - DataTables 和 jQuery 在表中执行 onclick 事件

标签 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>

最佳答案

使用下面的代码:

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

关于javascript - DataTables 和 jQuery 在表中执行 onclick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043505/

相关文章:

javascript - 打印 html 时页面范围的选项

Jquery Datatable 总和条件页脚回调不显示正确的结果

jquery - 文本区域中的字数始终返回 11

jquery mouseenter mouseleave 带数据表,在 chrome 或 IE 中不起作用

javascript - 如何在jquery数据表中添加复选框?

javascript - 用计数替换字符

javascript - 关于 app.listen(port, hostname) 和 server.createServer() 的混淆

javascript - 如何使用 javascript 获取行中 HTML 表格单元格的 Dropdownlist 值

javascript - Bootstrap 4 中的选项卡折叠

javascript - 当从 ajax 中单击“下一步”时,数据表会获取接下来的 10 个项目