Javascript 如何在选中或未选中复选框的基础上添加/删除项目?

Javascript 如何在选中或未选中复选框的基础上添加/删除项目?,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我已经写了以下几行代码 <tbody> <tr> <td><input class="commoncheckbox disabled-check" id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td> <td>abc</td> </tr> <tr> &l

我已经写了以下几行代码

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}
var selectedFinal=[];
//单击“检查所有记录”
$(文档)。在“单击”、“完成批次、.commoncheckbox”的函数()上{
var checkBoxCount=0;
所选var=[];
$(“#选定的#U学生”).empty();
$('#studentListBody输入:选中')。不是(“.disabled check”)。每个(函数(){
var id=$(this.val();
如果($(“#”+id).is(“:checked”)){
checkBoxCount++;
已选择。推送(id);
}否则{
支票箱计数--;
已选择。pop(id);
}
});
$(“#所有学生ID”).val($.unique(选定));
});

abc
abc
abc

为什么不在选中的
复选框中简单地使用
map()
get()

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}
var selectedFinal=[];
//单击“检查所有记录”
$(文档)。在(“单击”、“#completebatch、.commoncheckbox”上,函数(){
selectedFinal=$('.commoncheckbox:checked')。map((i,el)=>{
返回$(el.attr('value'))
}).get();
console.log(selectedFinal);
});

abc
abc
abc

为什么不在选中的
复选框中简单地使用
map()
get()

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}
var selectedFinal=[];
//单击“检查所有记录”
$(文档)。在(“单击”、“#completebatch、.commoncheckbox”上,函数(){
selectedFinal=$('.commoncheckbox:checked')。map((i,el)=>{
返回$(el.attr('value'))
}).get();
console.log(selectedFinal);
});

abc
abc
abc

请尝试执行以下脚本:

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

abc
abc
abc
选定=[];
功能检查(a){
if(document.getElementById(a).checked=true){
selected.push(document.getElementById(a).value);
}
}

请尝试执行以下脚本:

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

abc
abc
abc
选定=[];
功能检查(a){
if(document.getElementById(a).checked=true){
selected.push(document.getElementById(a).value);
}
}

我还修复了这个JSFIDLE上的代码:

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

我还修复了这个JSFIDLE上的代码:

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}