简体   繁体   English

使用JavaScript问题检查复选框状态

[英]Problem checking checkbox status using javascript

I am not very comfortable using javascript so trying my hands to use this in my one project,i have a small work which i have to impliment using javascript. 我对使用javascript不太满意,因此尝试在我的一个项目中使用它,我要做的小工作必须使用javascript。 on my JSP page i need to display a list of items which checkbox against each item and need to track if user select a item or deselecting an item. 在我的JSP页面上,我需要显示一个项目列表,每个项目都对应该复选框,并且需要跟踪用户是选择一个项目还是取消选择一个项目。 here is what i am trying 这是我正在尝试的

<input type="checkbox" value="someval" name="SelectedDestinationUUID" id="SelectedDestinationUUID" onchange="maintainCheckBoxStatus(this.value,'SelectedDestinationUUID')">

here is my javascript 这是我的javascript

function maintainCheckBoxStatus(checkBoxValue,checkBoxName){

    if(document.getElementById(checkBoxName).checked){
        alert("check");
        addCheckBoxStatus(checkBoxValue,true);
    }
    else{
        alert("check1");
        addCheckBoxStatus(checkBoxValue,false);
    }
}

now what happening is when i am selecting items it showing checked correctly but when unselecting the behaviour is something different here are the details let's suppose i have two items and i selected first than second and while deselecting i deselecting in the following order 现在发生的事情是当我选择项目时它显示正确,但是当取消选择行为时,这里有所不同,下面是详细信息,让我们假设我有两个项目,并且我选择了第一项而不是第二项,而取消选择时我按以下顺序取消选择

  1. deselecting item 1 取消选择项目1

  2. deselecting item 2 everything is working fine it showing checked and unchecked status fine. 取消选择第2项,一切正常,显示已选中和未选中状态。

but if I follow the following order for deselecting 但是如果我按照以下顺序取消选择

  1. deselecting item 2 取消选择项目2

  2. deselecting item 1 取消选择项目1

Things got messed while deselcting item two the alert showing me that its status is selected. 在选择第二项警报时,事情变得一团糟,向我显示其状态已选中。 can any one point me where i am doing wrong 谁能指出我做错了什么

Thanks in advance 提前致谢

Firstly please do 首先请

<input type="checkbox" value="someval" 
name="SelectedDestinationUUID" id="SelectedDestinationUUID" 
onchange="maintainCheckBoxStatus(this)">

function maintainCheckBoxStatus(checkBox){
    addCheckBoxStatus(checkBox.value,checkBox.checked);
}

which is the same as 这与

<input type="checkbox" value="someval" 
name="SelectedDestinationUUID" id="SelectedDestinationUUID" 
onchange="addCheckBoxStatus(this.value,this.checked)">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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