admin管理员组

文章数量:1291251

I have some issues. I have checkbox in my jsp page. So if the checkbox is checked i have to set a boolean variable to true else false. I tried something like this

<script>
$(document).ready(function() {
    $(".confirmCheckbox").change(function() {
        boolean confirmAssignee = false;
        $(this).attr("checked") {
            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });
});

But it is giving this error

SyntaxError: missing ; before statement
$(this).attr("checked") {

I have mutliple checkboxes on the page. Sample code is like this :

for loop(----)
<input type="checkbox" id="confirmBox-${loopCounter.index}" class = "confirmCheckbox">

Any help will be appreciated. Ask anything if required.

I have some issues. I have checkbox in my jsp page. So if the checkbox is checked i have to set a boolean variable to true else false. I tried something like this

<script>
$(document).ready(function() {
    $(".confirmCheckbox").change(function() {
        boolean confirmAssignee = false;
        $(this).attr("checked") {
            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });
});

But it is giving this error

SyntaxError: missing ; before statement
$(this).attr("checked") {

I have mutliple checkboxes on the page. Sample code is like this :

for loop(----)
<input type="checkbox" id="confirmBox-${loopCounter.index}" class = "confirmCheckbox">

Any help will be appreciated. Ask anything if required.

Share Improve this question edited May 15, 2015 at 7:09 Satpal 133k13 gold badges167 silver badges170 bronze badges asked May 15, 2015 at 7:04 user3681970user3681970 1,2815 gold badges21 silver badges39 bronze badges 2
  • use var, instead of boolean – Amit Soni Commented May 15, 2015 at 7:05
  • js var definition: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Irvin Dominin Commented May 15, 2015 at 7:09
Add a ment  | 

5 Answers 5

Reset to default 3

You need to use var while defining variable and use this.checked or .prop() to get whether checkbox is checked or not.

Simple statement will do it.

var confirmAssignee = this.checked; //OR $(this).prop("checked");

Complete Script

$(document).ready(function() {
    $(".confirmCheckbox").change(function() {
        var confirmAssignee = false;

        if (this.checked) {
            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });
});

There is not boolean type use var

Change

boolean confirmAssignee = false;

To

var confirmAssignee = false;

You also miss the if in the state $(this).attr("checked") { it should be if($(this).attr("checked")) {

Note use prop for checkbox instead of attr as suggest be jQuery doc.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

You can simplify code like

Live Demo

$(".confirmCheckbox").change(function () {   
    alert(this.checked);
});

Javascript have auto data types you didn't need to declare data types it is automatically taken in javascript. To check the checkbox using this.checked

 $(".confirmCheckbox").change(function () {
        var confirmAssignee = false;

        if (this.checked) {

            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });

Two problem in your code one there is no boolean datatype in javascript use var instead of boolean

secondly you need to check if checkbox is checked or not in if condition

use below code:

    $(document).ready(function() {

$( ".confirmCheckbox" ).change(function() {

var confirmAssignee = false;
if($(this).attr("checked")) {

}
alert(confirmAssignee);
});
 });

1)use var instead of of boolean

var confirmAssignee = false;

2)use checkbox checked property

if($(this).is(':checked')) 

本文标签: javascriptboolean variable in jqueryStack Overflow