如何根据各种复选框状态执行操作?

时间:2016-04-26 12:26:58

标签: javascript html html5 checkbox

我想根据复选框状态调用JavaScript函数。

例如,如果只有复选框1处于活动状态,我想调用函数genPDF()

如果所有复选框都处于活动状态,我想在按下生成报告按钮等后调用函数gen4PDF()

据我所知,我可以这样做:(并且有效) <a href="javascript:genPDF()"> DOWNLOAD PDF </a>

但我想要一个更“智能”的链接/按钮。我该怎么办?

我的代码框架:

<!DOCTYPE>
<html>
<head>
    <title>System Overview Report</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" src="jspdf.min.js"></script>
    <script type="text/javascript">


        function genPDF(){

        }

        function gen2PDF(){

        }

        function gen3PDF(){

        }

        function gen4PDF(){

        }

    </script>
</head>
<body>

    <input type="checkbox" name="basic">I want basic report<br>
    <input type="checkbox" name="network">I want network report<br>
    <input type="checkbox" name="suggestions">I want suggestions report<br>
    <input type="checkbox" name="complete">I want a complete report

    <a href="javascript:genPDF()"> GENERATE PDF REPORT </a>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

 var checkBoxes=document.getElementsByClassName('pdf');
for(var i=0; i<4;i++){
    checkBoxes[i].onclick=function(){
        var checkedNum=0;
        for(var j=0; j<4;j++){
            if(checkBoxes[j].checked){
                checkedNum++
            }
        }

        switch (checkedNum){

            case 1:
                genPDF();
                break;
            case 2:
                gen2PDF();
                break;
            case 3:
                gen3PDF();
                break;
            case 4:
                gen4PDF();
                break
        }
    }
}

答案 1 :(得分:0)

您可以获取所有复选框状态:

var options = {}
$('input[type="checkbox"]').each(function() {
  options[this.name] = this.checked
})

您可以检查单个或多个选项以确定要执行的操作。

&#13;
&#13;
genPDF = function() {
  var options = {}
  $('input[type="checkbox"]').each(function() {
    options[this.name] = this.checked
  })
  
  if (options.basic && !options.network) {
    // etc
  } else if () {
    // etc
  }
}
&#13;
<body>

    <input type="checkbox" name="basic">I want basic report<br>
    <input type="checkbox" name="network">I want network report<br>
    <input type="checkbox" name="suggestions">I want suggestions report<br>
    <input type="checkbox" name="complete">I want a complete report

    <a href="javascript:genPDF()"> GENERATE PDF REPORT </a>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

两个案例的代码第一个复选框和所有复选框:

function genPDF() {
  console.log('genPDF');
}

function gen2PDF() {
  console.log('gen2PDF');
}

function gen3PDF() {
  console.log('gen3PDF');
}

function gen4PDF() {
  console.log('gen4PDF');
}
function generate() {
  var basic = document.querySelector('[name="basic"]').checked;
  var network = document.querySelector('[name="network"]').checked;
  var suggestions = document.querySelector('[name="suggestions"]').checked;
  var complete = document.querySelector('[name="complete"]').checked;
  if (basic && !network && !suggestions && !complete) {
    genPDF();
  } else if (basic && network && suggestions && complete) {
    gen4PDF();
  }
}
<input type="checkbox" name="basic">I want basic report<br>
<input type="checkbox" name="network">I want network report<br>
<input type="checkbox" name="suggestions">I want suggestions report<br>
<input type="checkbox" name="complete">I want a complete report

<a href="javascript:generate()"> GENERATE PDF REPORT </a>