简体   繁体   English

需要我的jQuery输入样式器一些帮助

[英]Need some help with my jQuery input styler

Here's the, stripped down, code: 这是简化的代码:

$(document).ready(function()
{
   var $input = $('input[type="checkbox"]');

   $input.wrap('<div class="stylecheck-container" style="display: inline" />').addClass('stylecheck').hide();
   $container = $input.parent('div.stylecheck-container');

   if ($container.parent('label').length > 0)
   {
      $container.append($container.parent('label').text() + ' ');
      $container.parent('label').replaceWith($container);
   }

   $input.change(function()
   {
      alert(1);
   });

   $container.click(function()
   {
      if ($input.is(':checkbox'))
      {
         if ($input.is(':checked'))
            $input.attr('checked', true);
         else
            $input.attr('checked', false);
      }
   });
}); 

The problem I'm having is that when I try and remove the parent label, the change no longer gets detected. 我遇到的问题是,当我尝试删除父标签时,不再检测到更改。

If I comment out the line 6 if statement, it works fine. 如果我将第6行if语句注释掉,则可以正常工作。

What's the deal? 这是怎么回事?

Worth a shot but have you tried replacing $input.attr('checked', false); 值得一试,但您是否尝试过替换$input.attr('checked', false); with $input.removeAttr('checked'); $input.removeAttr('checked'); ? Because you're still setting it. 因为您仍在设置它。

Though I'm very sleepy so I haven't properly parsed all the code, but I see this little thing.. off to sleep. 尽管我很困,所以我没有正确解析所有代码,但是我看到了这个小东西。

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

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