简体   繁体   English

为什么不能单击这个用jQuery动态创建的复选框?

[英]Why can't this checkbox, created dynamically with jQuery, be clicked?

jsFiddle 的jsfiddle

I'm using a jQuery plugin that allows the user to draw boxes in an area. 我正在使用一个允许用户在一个区域中绘制框的jQuery插件。 I use jQuery to put a checkbox (along with a dropdown list) in the box that appears when the user lets go of the mouse button (this is towards the bottom of the javascript in the jsFiddle). 我使用jQuery在用户放开鼠标按钮时出现的框中放置一个复选框(以及下拉列表)(这是jsFiddle中javascript的底部)。 The problem is, the checkbox is unclickable. 问题是,复选框无法点击。

I do have some click checking code in the _mouseStart , _mouseDrag and _mouseStop events to stop another box from being created when you click in an existing box, but I don't think this is causing the problem because the dropdown list that is created can be clicked, and furthermore if you remove the click checking code the checkbox remains unclickable. 我确实在_mouseStart_mouseDrag_mouseStop事件中有一些点击检查代码,以阻止在您单击现有框时创建另一个框,但我认为这不会导致问题,因为创建的下拉列表可以是单击,此外,如果您删除点击检查代码,则复选框仍然无法点击。

What is causing the checkbox to be unclickable? 是什么导致复选框无法点击? Thanks for reading. 谢谢阅读。

EDIT: Thanks to VinayC's answer, I can now see that the click reaches the checkbox, with this code: 编辑:感谢VinayC的回答,我现在可以看到点击到达复选框,使用以下代码:

$('#box').click(function(e){
    alert('clicked');
    $(this).attr('checked', true);
});

But the $(this).attr('checked', true); 但是$(this).attr('checked', true); line doesn't make the checkbox checked. line不会选中复选框。 Can anyone tell me why? 谁能告诉我为什么? I've updated the jsFiddle 我已经更新了jsFiddle

EDIT 2: Harmen noticed that the code assigns the same id to each checkbox. 编辑2:Harmen注意到代码为每个复选框分配了相同的id。 In the actual code there's a counter appended to the id, so each one is unique, but I've taken that out because I think this is just a jQuery issue. 在实际的代码中有一个附加到id的计数器,所以每个都是唯一的,但我已经把它拿出来因为我认为这只是一个jQuery问题。 I'd change the jsFiddle, but if you just create one box (thus one checkbox), the same problem occurs. 我会改变jsFiddle,但是如果你只创建一个盒子(因此一个复选框),就会出现同样的问题。

I've got no idea why, but while fiddling around (yes, on fiddlejs), this seems to do the trick 我不知道为什么,但在摆弄(是的,在fiddlejs),这似乎做的伎俩

  $('#box', ui.box).click(
      function(evt){
          evt.stopPropagation();
      }
   );

when setting up the box. 设置盒子时。 See: http://jsfiddle.net/BBh3r/9/ 请参阅: http//jsfiddle.net/BBh3r/9/

I was actually trying to intercept the event and manually set it checked, but if there's no need to set it then hey.. Perhaps there's an extra event generated somewhere negating the first..? 我实际上试图拦截事件并手动设置它检查,但如果没有必要设置它然后嘿..也许有一个额外的事件生成某处否定第一个...? Click's only triggered once though. 点击仅触发一次。

Might be related to building jquery checkbox - can't set checked value 可能与构建jquery复选框有关 - 无法设置选中的值

PS. PS。 Only tested on Chrome for Linux 仅在Chrome for Linux上测试过

It appears that top level event handlers are cancelling the click event. 顶级事件处理程序似乎取消了click事件。 Add onclick event handler on check-box element alerting and you will see that click reaches to the checkbox. 在复选框元素警报上添加onclick事件处理程序,您将看到单击到达复选框。

您正在创建具有相同id多个复选框。

Actually it is checked while the alert is visible, but it becomes unchecked afterwards. 实际上,在警报可见的情况下进行检查,但之后它将被取消选中。 I'm guessing that after your event handler sets it to checked, the default event for the click (which is to toggle the check mark) happens, and since it is checked at the moment, it becomes unchecked again. 我猜测在事件处理程序将其设置为选中后,将发生单击的默认事件(即切换复选标记),并且由于此时已进行检查,因此它将再次取消选中。 Try calling preventDefault from the click handler. 尝试从单击处理程序调用preventDefault

You can also try this for a more universal approach 您也可以尝试使用更通用的方法

This worked for me. 这对我有用。

$(document).click(function (e) {
   if (element.tagName == 'INPUT') {
      if ($(element).attr("type") == 'checkbox') {
         e.stopPropagation();                                
         e.preventBubble();                             
         return;
      }
   }
});

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

相关问题 无法返回动态创建的复选框的值 - Can't return value of dynamically created checkbox 为什么单击鼠标后看不到动态创建的按钮上的数据字段? - Why can't I see the data-field on a dynamically created button when clicked? JavaScript:无法动态重置动态创建的复选框的复选框值 - JavaScript: Can't dynamically reset checkbox values for dynamically created checkboxes 点击处理程序,用于在jquery中动态创建按钮 - Clicked handler for dynamically created buttons in jquery jQuery动态创建的复选框无法正常使用功能 - jquery dynamically created checkbox not working properly with function jQuery / js删除动态创建的复选框 - jQuery/js Removing a dynamically created checkbox 如何增强动态创建的Jquery Mobile复选框? - How to enhance a dynamically created Jquery Mobile checkbox? 动态创建复选框单击事件选择整行,而不是仅选择jquery数据表中的复选框。 这是为什么? - Dynamically created checkbox click event selecting the whole row instead of only the check box in a jquery datatable. Why is that? 为什么在jquery中为动态创建的复选框定义属性的顺序会影响其值? - Why does order of defining attributes for a dynamically created checkbox in jquery affect its value? jQuery无法获取动态创建的父元素 - jQuery can't get elements parent which is created dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM