简体   繁体   English

jQuery触发触发点击事件,但不发送事件数据

[英]jQuery trigger firing click event but not sending event data

After searching for hours, I cannot seem to figure out why a jQuery triggered click event is not passing the actual event to the click function. 在搜索了几个小时之后,我似乎无法弄清楚为什么jQuery触发的click事件没有将实际事件传递给click函数。 Here is an example: jsfiddle . 这是一个示例: jsfiddle

$(document).ready(function () {
    $('tr').click(function (event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger("click");
        }
    });

    $(':checkbox').click(function (event) {
        console.log(event.shiftKey);
    });
});

If I click the first checkbox, the console logs "false" as expected. 如果单击第一个复选框,控制台将按预期记录“ false”。 Then, if I hold the shift key followed by checking the last checkbox, the console logs "true" as expected because as that box was clicked, shift was being held. 然后,如果按住Shift键,然后选中最后一个复选框,则控制台将按预期方式记录“ true”,因为单击该框时,将按住shift键。

Now move over to the data and click on rows rather than checkboxes. 现在移至数据,然后单击行而不是复选框。 Regardless of whether or not the shift key is being held as a row is clicked, the checkbox click event is fired, the box is checked/unchecked as appropriate, but event.shiftKey always returns false. 无论是否在单击行时都按住Shift键,都会触发复选框click事件,并在适当情况下选中/取消选中该框,但是event.shiftKey始终返回false。

Why does triggering a click event from the row result in the originating event not being passed to the click handler? 为什么从该行触发click事件会导致原始事件没有传递给click处理程序?

simulating mouse click fully is not as straightforward as it seems. 完全模拟鼠标单击并不像看起来那样简单。 Here you can find detailed code for it (w/o using jQuery) : https://stackoverflow.com/a/6158050/405623 在这里您可以找到它的详细代码(不使用jQuery): https : //stackoverflow.com/a/6158050/405623

From functional point you could maybe have function you want to run on any click in row and inside define what kind of event it was. 从功能上讲,您可能具有要在任何单击行中运行的功能,并且可以在其中定义事件类型。 I mean get rid of generating events towards handling them in one place. 我的意思是摆脱在一个地方处理事件的事件生成。

You're triggering a new event on the checkbox, and not passing on the original. 您在复选框上触发了一个新事件,而不传递原始事件。 In that event, no shift is pressed. 在这种情况下,不按任何键。 I would use a named function to avoid this confusion, like so: 我将使用命名函数来避免这种混淆,如下所示:

$(document).ready(function () {
    $('tr').click(function (event) {
        console.log('Row clicked');
        eventHandler(event, this);
    });

    $(':checkbox').click(function (event) {
        event.stopPropagation();
        console.log('Checkbox clicked');
        eventHandler(event, this);
    });

    function eventHandler(event, elem){
        console.log(event.shiftKey);
        $(':checkbox', elem).click();
    }
});

Fiddle: http://jsfiddle.net/4hfwsh5q/7/ 小提琴: http : //jsfiddle.net/4hfwsh5q/7/

stopPropagation() also prevents the event from being passed further down when you click the checkbox, so a click on the row won't be triggered. 当您单击复选框时,stopPropagation()还可防止事件进一步传递,因此不会触发对该行的单击。

Update Here's another fiddle that only checks/unchecks the checkbox if shift is being held: http://jsfiddle.net/4hfwsh5q/8/ 更新这里是另一个小提琴,仅在按住Shift键时才选中/取消选中该复选框: http : //jsfiddle.net/4hfwsh5q/8/

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

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