简体   繁体   English

jQuery切换不适用于窗体上的图像/复选框

[英]jQuery toggle not working for image/checkbox on form

I've got a form setup that's using DIVs as the toggles for checkboxes. 我有一个使用DIV作为复选框切换的表单设置。 I've got some jQuery code that watches the DIVs for a click, and then toggles the class to change the image. 我有一些jQuery代码,可以观看DIV的单击,然后切换类以更改图像。 Easy peasy, right? 轻轻松松吧?

I had everything working great, but when I use the checkbox coupled with the product option pricing (as dictated by the plugins for the form & cart I'm using), the jQuery doesn't work. 我的一切工作都很好,但是当我使用复选框以及产品价格选项(由我使用的表格和购物车的插件规定)时,jQuery不起作用。

I assumed that there was another class, or something associated with the pricing construct, overriding the code, but the DIVs look identitcal between the working and non-working version. 我以为还有另一个类或与定价结构相关联的某个类覆盖了代码,但是DIV在工作版本和非工作版本之间看起来是相同的。 I'm at a loss as to what's happening—the jQuery should still work based on what I can see. 我对正在发生的事情一无所知-jQuery应该仍然基于我所看到的工作。

You can check out the form here: http://bit.ly/YZBdbc 您可以在此处签出表格: http : //bit.ly/YZBdbc

I've got both checkbox examples posted so you can see the difference (under "Choose your options"). 我已经发布了两个复选框示例,以便您可以看到不同之处(在“选择选项”下)。 The top one works as I'd like visually but doesn't add pricing, the bottom one doesn't toggle, but the pricing works. 最上面的一个按我的意愿运行,但不添加定价,最下面的一个不切换,但是定价有效。 Sigh. 叹。

Thanks in advance for your help! 在此先感谢您的帮助!

EDIT: Here's the jQuery code: 编辑:这是jQuery代码:

jQuery('#cart_option_1').click(function() {
    jQuery(this).toggleClass('active');
});
    jQuery('#cart_option_2').click(function() {
    jQuery(this).toggleClass('active');
});
    jQuery('#cart_option_3').click(function() {
    jQuery(this).toggleClass('active');
});

Here's the DIV code from the working block: 这是工作块中的DIV代码:

<div class='ginput_container'><ul class='gfield_checkbox' id='input_1_25'><li class='gchoice_25_1'><input name='input_25.1' type='checkbox' value='Month-to-Month +$10.00' id='choice_25_1' tabindex='2' /><label for='choice_25_1'><div id="cart_option_1b"></div></label></li>

And the code from the non-working block (which is basically identical): 以及来自非工作块的代码(基本上是相同的):

<div class='ginput_container'><ul class='gfield_checkbox' id='input_1_26'><li class='gchoice_26_1'><input name='input_26.1' type='checkbox' value='Month-to-month|10' id='choice_26_1' tabindex='5' /><label for='choice_26_1'><div id="cart_option_1"></div></label></li>

Just make it a class. 只要上一堂课。

<div class="cart_option" id="cart_option_1"></div>

And then apply the click event to the class. 然后将click事件应用于该类。

$('.cart_option').on('click',function(){
    $(this).toggleClass('active');
});

Edited for clarification: You can still have the unique images applied to cart_option_1 , cart_option_1b , whatever, but the click event applies to all because they all have the class associated. 为澄清cart_option_1进行了编辑:仍然可以将唯一的图像应用于cart_option_1cart_option_1b ,但是,单击事件将应用于所有对象,因为它们都具有关联的类。

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

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