简体   繁体   中英

Uncaught Syntax Error: Unexpected token

I am still learning jQuery. I am trying to write a script that finds any element with rel="toggleSelect". It should add an onclick attribute with my function that enables or disables all checkboxes in the provided group("actions" array). I am getting an error that says "Uncaught Syntax Error: Unexpected token }". I cannot figure out why. I think it has something to do with the selector for the groupId perm input. TYIA!!!

$(function(){
        $('input[rel="toggleSelect"]').on("click", function(){
                var groupId = $(this).attr("id");
                var actions = new Array("read", "create", "reply", "moderate");
                for(var i=0;i<=actions.length;i++){
                    checkboxName = $('input[name="perms[" + groupId + "][" + actions[i] + "]"');
                    if(checkboxName.is(":enabled")){
                        checkboxName.prop("disabled", true);
                    } else {
                        checkboxName.prop("disabled", false);
                    }
                }
        }
    });

An example of the input:

<input type="checkbox" name="perms[1][read]" value="1" checked="checked">

Once you fix the missing parenthesis when binding your click event handler, there's a problem with this line:

checkboxName = $('input[name="perms[" + groupId + "][" + actions[i] + "]"');

That looks as though you're attempting to dynamically build the string by concatenating the values of some variables with string literals, but you're using the wrong type of quotes to end the string before concatenating. The use of double quotes, when you actually need single, results in a completely invalid jQuery selector.

Change it to the following:

checkboxName = $('input[name="perms[' + groupId + '][' + actions[i] + ']"');

You're missing a closing ) . Also, you need to instantiate your checkboxName by var keyword as well as concatenating your selectors other way, final code should look like:

$(function () {
    $('input[rel="toggleSelect"]').on("click", function () {
        var groupId = $(this).attr("id");
        var actions = new Array("read", "create", "reply", "moderate");
        for (var i = 0; i <= actions.length; i++) {
            var checkboxName = $('input[name="perms[' + groupId + '][' + actions[i] + ']"');
            if (checkboxName.is(":enabled")) {
                checkboxName.prop("disabled", true);
            } else {
                checkboxName.prop("disabled", false);
            }
        }
    }); // <-- here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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