admin管理员组

文章数量:1287775

I'm trying to limit the number of checkboxes that can be checked within a form, but am getting the following error:

Uncaught Error: Syntax error, unrecognized expression: input:checkbox[name=ninja_forms_field_57[terms]]

Here is the Javascript that I'm using:

// Main Category
var $checkboxes_to_limit2 = $("#taxonomy_47").find("input:checkbox[name=ninja_forms_field_47[terms]]");
$checkboxes_to_limit2.live("change", function() {
    if($checkboxes_to_limit2.filter(":checked").length >= 3) {
    $checkboxes_to_limit2.not(":checked").attr("disabled","disabled");
    }
    else {
        $checkboxes_to_limit2.removeAttr("disabled");
    }
});

I'm trying to limit the number of checkboxes that can be checked within a form, but am getting the following error:

Uncaught Error: Syntax error, unrecognized expression: input:checkbox[name=ninja_forms_field_57[terms]]

Here is the Javascript that I'm using:

// Main Category
var $checkboxes_to_limit2 = $("#taxonomy_47").find("input:checkbox[name=ninja_forms_field_47[terms]]");
$checkboxes_to_limit2.live("change", function() {
    if($checkboxes_to_limit2.filter(":checked").length >= 3) {
    $checkboxes_to_limit2.not(":checked").attr("disabled","disabled");
    }
    else {
        $checkboxes_to_limit2.removeAttr("disabled");
    }
});
Share Improve this question asked Oct 20, 2013 at 22:42 adsfadsf 591 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You need to treat the name attribute as a string, so

input:checkbox[name=ninja_forms_field_47[terms]]

should be

input:checkbox[name='ninja_forms_field_47[terms]']

You need some additional single quotes:

var $checkboxes_to_limit2 =
  $("#taxonomy_47").find("input:checkbox[name='ninja_forms_field_47[terms]']")

本文标签: