简体   繁体   English

单击事件不适用于 chrome(jQuery) 中动态插入的元素

[英]click event not working on dynamically inserted element in chrome(jQuery)

What I do here is that I dynamically create a drop-down and bind the click event to the <option> within the <select>我在这里做的是我动态创建一个下拉列表并将click事件绑定到<select><option> <select>

var $select = $('<select></select>'),
    $option1 = $('<option>a</option>'),
    $option2 = $('<option>b</option>');

    $option1.val(1);
    $option2.val(2);

$(document.body).on('click','select option',function(){
      console.log("click-option works");
    });

    $select.append($option1);
    $select.append($option2);

    $(document.body).append($select);

As I read here , Since I am dynamically adding content, I delegated the event handling to the top most element in the DOM.正如我在这里读到的,由于我是动态添加内容,因此我将事件处理委托给 DOM 中最顶层的元素。

Now my problem is this is not working in chrome as expected.现在我的问题是这在 chrome 中没有按预期工作。 In fire-fox it seems to work fine.在火狐中它似乎工作正常。

I have also read through this link and the other one but to no avail.我也通读了这个链接和另一个链接,但无济于事。

Interestingly when I do this in the place of 'select option' selector有趣的是,当我代替'select option'选择器执行此操作时

$(document.body).on('click','select',function(){
      console.log("click-option works");
    });

The output is logged, However that is definitely not what I had in mind.输出已记录,但这绝对不是我的想法。 The output is logged whenever I select the drop-down itself, not to mention the options in the drop-down.每当我选择下拉菜单时都会记录输出,更不用说下拉菜单中的选项了。 As a temporary work around I am using change event in the following way :作为临时解决方法,我通过以下方式使用change事件:

$(document.body).on('change','select',function(event){
      console.log("change works");
      console.log(event);
    });

This seems to do the trick,However I want to know why It is not working in the original case and any work-arounds if possible.这似乎可以解决问题,但是我想知道为什么它在原始案例中不起作用,如果可能,还有任何变通方法。

Here is a fiddle for what I have done so far.这是我迄今为止所做的事情的小提琴

UPDATE : I have also checked out this link .更新:我也检查了这个链接 However that is for just pure javascript, I wanted to know why it snot working in my case.然而,这仅适用于纯 javascript,我想知道为什么它在我的情况下不起作用。 More specifically, in the delegated event handler where we specifiy the selector, Why does it fail at 'select option' and work at 'select' in chrome ?更具体地说,在我们指定选择器的委托事件处理程序中,为什么它在'select option'处失败而在 chrome 中的'select'处工作?

The issue is because you're attaching a click event to the option elements which isn't very reliable.问题是因为您将click事件附加到不太可靠的option元素。 The click event should go on the select itself - but note that even that isn't following best practices as you should use the change event so that your code is fired even when the option is selected via the keyboard. click事件应该继续select本身 - 但请注意,即使这不遵循最佳实践,因为您应该使用change事件,以便即使通过键盘选择该选项也会触发您的代码。 To get the value of the selected ooption within the handler use $(this).val() , like this:要获取处理程序中所选 ooption 的值,请使用$(this).val() ,如下所示:

 var $select = $('<select></select>'), $option1 = $('<option>a</option>'), $option2 = $('<option>b</option>'); $option1.val(1); $option2.val(2); $(document).on('change', 'select', function() { console.log($(this).val()); }); $select.append($option1); $select.append($option2); $('body').append($select);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Also note that this can be shortened to just:另请注意,这可以缩短为:

 $(document).on('change', 'select', function() { console.log($(this).val()); }); $('<select><option value="1">a</option><option value="2">b</option></select>').appendTo('body');
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

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