javascript jquery中的布尔变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30253482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:57:07  来源:igfitidea点击:

boolean variable in jquery

javascriptjqueryjsp

提问by user3681970

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

我有一些问题。我的jsp页面中有复选框。因此,如果选中该复选框,我必须将布尔变量设置为 true 否则为 false。我试过这样的事情

<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.

任何帮助将不胜感激。如果需要,请询问任何事情。

回答by Satpal

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

您需要var在定义变量时使用,并使用this.checked.prop()获取复选框是否被选中。

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);
    });
});

回答by Adil

There is not booleantype use var

没有布尔类型使用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")) {

你也错过了$(this).attr("checked") {它应该是的状态if($(this).attr("checked")) {

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

注意使用 prop 作为复选框而不是 attr 作为建议是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.

例如,应该使用 .prop() 方法检索和设置 selectedIndex、tagName、nodeName、nodeType、ownerDocument、defaultChecked 和 defaultSelected。在 jQuery 1.6 之前,这些属性可以使用 .attr() 方法检索,但这不在 attr 的范围内。这些没有相应的属性,只是属性。

You can simplify code like

您可以简化代码,如

Live Demo

现场演示

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

回答by Sudharsan S

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

Javascript 具有自动数据类型,您不需要声明它在 javascript 中自动采用的数据类型。使用检查复选框 this.checked

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

        if (this.checked) {

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

回答by Vivek Gupta

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

您的代码中有两个问题,一是 javascript 中没有布尔数据类型,请使用 var 而不是 boolean

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

其次,您需要检查复选框是否在 if 条件下被选中

use below code:

使用以下代码:

    $(document).ready(function() {

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

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

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

回答by Nagaraj S

1)use varinstead of of boolean

1)使用var代替布尔值

var confirmAssignee = false;

var confirmAssignee = false;

2)use checkbox checked property

2)使用复选框选中的属性

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