javascript 选中复选框的显示值javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22292649/
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-27 22:52:08  来源:igfitidea点击:

Display value of checked check boxes javascript

javascriptcheckbox

提问by VIJU

Here I have written a javascript which selects all checkboxes on checking one checkbox and I want to display all the checked checkboxes value on button click. here it does selectall function correctly(ie. it selects all checkboxes). I am new to javascript and I need some help to display all the checked check box values, can any any one provide me the code to select all checkbox by clicking on a check box and display values of only selected checkboxes in a single function using javascript only...

在这里,我编写了一个 javascript,它在选中一个复选框时选择所有复选框,我想在单击按钮时显示所有选中的复选框值。在这里它正确地执行 selectall 功能(即它选择所有复选框)。我是 javascript 新手,我需要一些帮助来显示所有选中的复选框值,任何人都可以通过单击复选框向我提供选择所有复选框的代码,并使用 javascript 在单个函数中仅显示选定复选框的值只要...

Here is the javascript code

这是javascript代码

<script>
var checked=false;
function checkedAll ()
{
  var c =  document.getElementsByName("viju");
  checked = document.getElementById('causelist_month').checked;

 for (var i =0; i < c.length; i++)
 {
  c[i].checked=checked;
 }
}
</script>

Here the HTML code

这里是 HTML 代码

 <input type="checkbox" name="causelist_month" id="causelist_month" onclick="checkedAll ();">select all/unselect all
 <input type="checkbox" name="viju" id="viju" value="Jan" onClick="">jan
 <input type="checkbox" name="viju" id ="viju" value="feb" onClick="">feb
 <input type="Button" value="Show values" onClick="checkedAll(this.value)"/>    

回答by Kyo

First, use the event listener for the checkboxes rather than onClick:

首先,对复选框使用事件侦听器而不是 onClick:

document.getElementById("causelist_month").addEventListener('change', function(){
    checkboxes = document.getElementsByName("viju");
    for( var i=0; i<checkboxes.length; i++){
       checkboxes[i].checked = this.checked;
    }      
}, false);

And for the display of the checked items, in HTML:

对于选中的项目的显示,在 HTML 中:

<input type="Button" value="Show values" onClick="displayChecked()"/>
<div id="display"></div>

Then, in javascript:

然后,在 javascript 中:

function displayChecked (){
    var display = "";
    checkboxes = document.getElementsByName("viju");
    for( var i=0; i<checkboxes.length; i++){
        if( checkboxes[i].checked ){
            display += " " + checkboxes[i].value;
        }
    }
    document.getElementById("display").innerHTML = display;
}

回答by SajithNair

Jsfiddle http://jsfiddle.net/2UFdc/

jsfiddle http://jsfiddle.net/2UFdc/

HTML

HTML

<form>
    <input type="checkbox" name="causelist_month" id="causelist_month" onclick="checkedAll ();">select all/unselect all
    <input type="checkbox" name="viju" id="viju" value="Jan" onClick="">jan
    <input type="checkbox" name="viju" id ="viju" value="feb" onClick="">feb
    <input type="Button" value="Show values" onClick="showVal(this.form)"/>  
</form>

Javascript

Javascript

var checked = false;

function checkedAll() {
    var c = document.getElementsByName("viju");
    checked = document.getElementById('causelist_month').checked;

    for (var i = 0; i < c.length; i++) {
        c[i].checked = checked;
    }
}

function showVal(frm) {
    var arr = [];
    for (var i in frm.viju) {
        if (frm.viju[i].checked) {
            arr.push(frm.viju[i].value);
        }
    }
    alert(arr);
    return arr
}

回答by reid

you could use jquery functions dont forget to inclde jquery library

你可以使用 jquery 函数不要忘记包含 jquery 库

<button id="showall"> display </button>`
$("#showall").click(function() {

<button id="showall"> display </button>`
$("#showall").click(function() {

         var array = [];
             $(':checkbox:checked').each(function(i){
                  array[i] = $(this).val();
                 });

        $.each( array, function( i, val ) {
        $("#display").append(val); //the div where you want to display
         });

             });

`

`

回答by SriSaiKiran

<!DOCTYPE html>
<html>
<script>
var checked=false;
function checkedAll()
{
   var c = document.getElementsByName("viju");
   var checkboxesChecked = [];
  // loop over them all
  for (var i=0; i<c.length; i++) {
  // And stick the checked ones onto an array...
  if (c[i].checked) {
    checkboxesChecked.push(c[i]);
      }
 }

if(document.getElementById('causelist_month').checked)
{
    checked = document.getElementById('causelist_month');
    checkboxesChecked.push(checked);
}

  for (var j=0; j<checkboxesChecked.length; j++) {
      // iterate the pushed values
       alert(checkboxesChecked[j].value);
 }

}
</script>
<body>
<form>
<input type="checkbox" name="causelist_month" id="causelist_month" value="select all/unselect all" onclick="checkedAll     ();">select all/unselect all
 <input type="checkbox" name="viju" id="viju" value="Jan" onClick="">jan
 <input type="checkbox" name="viju" id ="viju" value="feb" onClick="">feb
 <input type="Button" value="Show values" onClick="checkedAll(this.value)"/>  
<form>
</body>
</html>

This displays all the list of check boxes in the alert message one by one. please check

这将一一显示警报消息中的所有复选框列表。请检查