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 ofboolean
– 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
5 Answers
Reset to default 3You 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
版权声明:本文标题:javascript - boolean variable in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741528259a2383598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论