简体   繁体   English

jQuery addClass和removeClass复选框

[英]jQuery addClass and removeClass in checkboxes

i am new to jQuery, and i am trying to figure out how to create the jQuery function of the checkboxes, what i want to attain is that, when a user clicked on Yes the checkboxes(which will have a display:none) will have a checkmark. 我是jQuery的新手,我想弄清楚如何创建复选框的jQuery函数,我想要达到的是,当用户点击是复选框(将有一个显示:无)将有选中标记。 But if a user will click on No the checkmark will be removed. 但是,如果用户点击“否”,则将删除复选标记。

This will be a selection of multiple Yes and multiple No 这将是多个是和多个选择

have a look at the page im working on http://jsfiddle.net/4x2f52ka/ 看一下我在http://jsfiddle.net/4x2f52ka/上工作的页面

I have started the script for it, but for some reason, when i started to select Yes on the first selection, another yes to the second selection, and if im gonna select No to the 3rd item, the 2 Yes will be removed. 我已经为它启动了脚本,但出于某种原因,当我开始在第一个选择上选择“是”时,对第二个选择选择“是”,如果我要选择“否”到第3个项目,则“是”将被删除。 It is fine if i will select all yes and all no, but if im gonna select alternate Yes and No, it doesnt allow me to. 如果我将选择所有是和所有否,这是好的,但如果我要选择替代是和否,它不允许我。

this is my code for the html 这是我的html代码

<div class="row marginBottom">
    <div class="col-xs-6">
        <label class="alignMid">Have you found a property?</label>
    </div>
    <div class="col-xs-6">
        <span class="opt_yes"><p style="line-height: 38px; font-size:15px;">YES</p></span>
        <span class="opt_no"><p style="line-height: 38px; font-size:15px;">NO</p></span>
        <input type="checkbox" name="FoundProperty"/>
    </div>
</div>  

<div class="row marginBottom">
    <div class="col-xs-6">
        <label class="alignMid">Would you like a free RP Data Property Report?</label>
    </div>
    <div class="col-xs-6">
        <span class="opt_yes"><p style="line-height: 38px; font-size:15px;">YES</p></span>
        <span class="opt_no"><p style="line-height: 38px; font-size:15px;">NO</p></span>
        <input type="checkbox" name="FreeRPData"  />
    </div>
</div>

and this is my code for the jQuery 这是我的jQuery代码

<script type="text/javascript"> 
jQuery(document).ready(function () { 
    jQuery('.opt_yes').click(function () {      
        jQuery(this).addClass('selected'); 
        jQuery('.opt_no').removeClass('selected')
            if (jQuery(this).hasClass('selected')) { 
            jQuery(this).parent().find("input[type='checkbox']").prop('checked', true); 
            } 
    }); 
    jQuery('.opt_no').click(function () {       
        jQuery(this).addClass('selected'); 
        jQuery('.opt_yes').removeClass('selected')
            if (jQuery(this).hasClass('selected')) { 
            jQuery(this).parent().find("input[type='checkbox']").prop('checked', false); 
            } 
    });     

}); 
</script>

Can anyone help me please? 有人可以帮我吗?

You're loosing the context of your check boxes. 您正在丢失复选框的上下文。 It works for yes, because you use this , but for the no items, you just grab every 'no box' on the document using jQuery('.opt_no') . 它适用于是,因为你使用this ,但对于没有项目,你只需使用jQuery('.opt_no')获取文档上的每个' jQuery('.opt_no') Because it's so broad, all 'no box' boxes are affected. 因为它如此广泛,所有“无盒子”盒子都会受到影响。

You can very easily narrow your selectors with this syntax: jQuery('selector', context) . 您可以使用以下语法轻松缩小选择器: jQuery('selector', context)

In this case, I recommend this.parentNode like so : 在这种情况下,我建议this.parentNode如下:

jQuery('.opt_yes', this.parentNode).removeClass('selected')

See my demo here: http://jsfiddle.net/m5aug81a/ 在这里看我的演示: http//jsfiddle.net/m5aug81a/

For more info on using contexts for your jQuery objects, see here 有关为jQuery对象使用上下文的更多信息, 请参阅此处

You have to remove selected class only from sibling of yes or no. 您必须仅从是或否的兄弟中删除所选的类。 Here's the code: 这是代码:

<script type="text/javascript"> 
jQuery(document).ready(function () { 
jQuery('.opt_yes').click(function () {      
    jQuery(this).addClass('selected'); 
    jQuery(this).siblings('.opt_no').removeClass('selected')
        if (jQuery(this).hasClass('selected')) { 
        jQuery(this).parent().find("input[type='checkbox']").prop('checked', true); 
        } 
}); 
jQuery('.opt_no').click(function () {       
    jQuery(this).addClass('selected'); 
    jQuery(this).siblings('.opt_yes').removeClass('selected');
        if (jQuery(this).hasClass('selected')) { 
        jQuery(this).parent().find("input[type='checkbox']").prop('checked', false); 
        } 
});     

}); 
</script>

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

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