简体   繁体   English

如何模拟数据表中的鼠标单击?

[英]How to simulate a mouse click in a datatables?

I would like to simulate a mouse click in my datatables (I'm using Datatables plugin to make the table) when I click on the Edit button to open the inline editor in my table. 当我单击“ Edit按钮以打开表中的内联编辑器时,我想在数据表中模拟鼠标单击(我正在使用Datatables插件制作表)。 For this I use $("#datatable td").trigger("click"); 为此,我使用$("#datatable td").trigger("click"); , it's work but with this method the inline editor open for each line of the table and I would like to open the inline on the line where the button is and not everywhere. ,它是可行的,但是使用此方法,可以为表的每一行打开内联编辑器,而我想在按钮所在的行而不是到处都打开内联。

JS : JS:

$("#datatable").on("click", "#btn_ed", function () {
    $("#datatable td").trigger("click");
});

在此处输入图片说明

Use closest to find the td belonging to the button: 使用closest来查找属于按钮的td

$(document.body).on('click','.editBtn', function() {
    $(this).closest('td').trigger("click");
});

This assumes your edit buttons have the class editBtn . 假定您的编辑按钮具有class editBtn

Also take note that I'm using the three-parameter method, which will make the click handler work even when new td s are being added later on. 还要注意,我使用的是三参数方法,即使稍后再添加新的td ,它也会使点击处理程序正常工作。

I see you added a code snippet to your question which uses an id to select the edit buttons. 我看到您在问题中添加了一个代码段,该代码段使用ID选择编辑按钮。 This is bad practice, the id of an element should be unique and the selector should apply to only one result. 这是一种不好的做法,一个元素的ID应该唯一,选择器只能应用于一个结果。 It is better to use a class for all edit buttons. 最好对所有编辑按钮使用一个类。

Try this, Hope it helps 试试这个,希望对您有所帮助

 $("#datatable").on("click", "#btn_ed", function () {

             $(this).closest('tr').find('td').each(function(e){

                   $(this).trigger('click')  

            })
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM