简体   繁体   English

jQuery - 未选中复选框

[英]jQuery - Checkbox not being checked

I'm using checkboxes to toggle the enabled and disabled state of some multi-lists on a registration form. 我正在使用复选框来切换注册表单上某些多列表的启用和禁用状态。 The checkbox is labeled with the category, and the multi-list contains the items that belong to that category. 该复选框标有类别,多列表包含属于该类别的项目。

I'm using jQuery 1.7.2. 我正在使用jQuery 1.7.2。

        $('#sch_cat_hockeyschools').toggle(function(ev) {
                ev.stopPropagation();
                $("#type_select_hockeyschools").prop("disabled", false);
                $("#type_select_hockeyschools").removeProp("disabled", "disabled");
                $("#sch_cat_hockeyschools").prop("checked", true);
                $("#sch_cat_hockeyschools").prop("checked", "checked");
            }, function(ev) {
                ev.stopPropagation();
                $("#type_select_hockeyschools option:selected").removeAttr("selected");
                $("#type_select_hockeyschools").prop("disabled", true);
                $("#type_select_hockeyschools").prop("disabled", "disabled");
                $("#sch_cat_hockeyschools").prop("checked", false);
                $("#sch_cat_hockeyschools").removeProp("checked");
            });

Sample of corresponding checkbox HTML: 相应复选框HTML的示例:

<input class="catmark" type="checkbox" name="sch_categories[]" id="sch_cat_hockeyschools" value="1" />General Hockey Schools
<input class="catmark" type="checkbox" name="sch_categories[]" id="sch_cat_springhockey" value="2" />Spring Hockey

The problem is that the upon clicking the checkbox, the checkbox does not become ticked or checked; 问题是,单击复选框后,复选框不会被勾选或选中; it immediately returns to an unchecked state, which I thought the stopPropagation() function would help with. 它会立即返回到未经检查的状态,我认为stopPropagation()函数会有所帮助。 Apparently not. 显然不是。 The multi-lists get enabled and disabled as expected, but the checkbox doesn't get ticked. 按预期启用和禁用多列表,但不勾选复选框。

The result of this problem is that when the form is submitted, the array containing the selected categories is empty; 此问题的结果是,在提交表单时,包含所选类别的数组为空; thus, because at least one checked category is a required field in the form, the PHP script that processes the form throws one of my errors which tells me a required field was left blank. 因此,因为至少有一个已检查的类别是表单中的必填字段,处理表单的PHP脚本会抛出我的一个错误,告诉我必填字段留空。

Any ideas on how to make sure that the checkbox actually gets checked, and by extension, POSTS actual data to the processing script? 关于如何确保复选框实际被检查的任何想法,以及通过扩展,POSTS实际数据到处理脚本?

Thanks guys. 多谢你们。

The problem is the use of toggle -- per the documentation : 问题是使用toggle - 根据文档

The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element. 该实现还在事件上调用.preventDefault(),因此如果在元素上调用了.toggle(),则不会遵循链接并且不会单击按钮。

toggle itself is calling preventDefault , which is stopping the default behavior of the event, checking/unchecking the box. toggle本身就是调用preventDefault ,它会停止事件的默认行为,检查/取消选中该框。

Rather than toggle, use bind or on ( see edit note below ) to add a listener for the change event, in which you can examine the state: 而不是切换,使用bindon请参阅下面的编辑说明 )为change事件添加一个侦听器,您可以在其中检查状态:

$('#sch_cat_hockeyschools').on('change', function () {
    if (this.checked) {
        // do stuff for a checked box   
        console.log('check is on');
    } else {
        // do stuff for an unchecked box        
        console.log('check is off');
    }
});

Try it out at jsFiddle . 在jsFiddle尝试一下

EDIT Please note, this code shows use of the on method, whereas the jsFiddle example uses bind . 编辑请注意,此代码显示使用on方法,而jsFiddle示例使用bind As pointed out by Sam Sehnert, on is the preferred method for attaching events with > jQuery 1.7. 正如Sam Sehnert所指出的, on是使用> jQuery 1.7附加事件的首选方法。 If you are using an older version of jQuery, use bind as in the jsFiddle example. 如果您使用的是旧版本的jQuery,请在jsFiddle示例中使用bind

Documentation 文档

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

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