PHP和JS-如何从选中的复选框中获取值并输入


PHP and JS - How to get value from chosen checkbox is checked and enter?

我只想创建一个动态值,该值将根据复选框选中的数量发送到警报弹出窗口

1-这意味着当复选框Test 1被选中按下enter按钮时,只需警报值="1"

2-当检查测试1、测试2和测试3以及按下回车键时,给出值="1,2,3"和相同的方式等。
3-然后,如果选中复选框的所有按enter,则会给出所有值="1,2,3,4"

这是我已经开始的函数,但我无法思考如何在函数中写入

  function checkTest(){
    // script and condition here that i'm stuck to begin
         alert(values);
        }

<input type="checkbox" id="st_1" class="cbgroup1" name="cbg1[]" value="1"> Test 1<br/> 
<input type="checkbox" id="st_2" class="cbgroup1" name="cbg1[]" value="2"> Test 2<br/> 
<input type="checkbox" id="st_3" class="cbgroup1" name="cbg1[]" value="3"> Test 3<br/>  
<input type="checkbox" id="st_4" class="cbgroup1" name="cbg1[]" value="4"> Test 4<br/> 
<input type="checkbox" id="cbgroup1_master" onchange="togglecheckboxes(this,'cbgroup1')"> Select All <br/>
<button type="button" onclick="javascript:checkTest();" class="btn btn-default"> Enter  </button>

编辑为正确。不得不拿出美元,因为它们不起作用,也不想排除故障。这将完全按照您的要求工作。不过只有jQuery迭代。需要使用Javascript更改某些方面。

    function checkTest(){
        // script and condition here that i'm stuck to begin
         var checkboxes = jQuery('.cbgroup1'), values = '';
         checkboxes.each(function(){
             if(jQuery(this).is(':checked') || jQuery('#cbgroup1_master').is(':checked')){
                 if(values != ''){
                    values += ',';
                 }
                    values += jQuery(this).val();
             }
         });
        alert(values);
    }

您可以使用以下代码检查复选框是否选中:

function checkTest(){
    // script and condition here that i'm stuck to begin
    var c1 = document.getElementById("st_1").checked;
    var c2 = document.getElementById("st_2").checked;
    var c3 = document.getElementById("st_3").checked;
    var c4 = document.getElementById("st_4").checked;
    var values = "";
    if(c1){
        //first check box is checked. you can add your desired code here
        values = values + "1";
    }
    if(c2){
        values = values + ",2";
    }
    if(c3){
        values = values + ",3";
    }
    if(c4){
        values = values + ",4";
    }
    alert(values);
}

或者,如果我误解了你的问题:

function checkTest(){
    // script and condition here that i'm stuck to begin
    var c1 = document.getElementById("st_1").checked;
    var c2 = document.getElementById("st_2").checked;
    var c3 = document.getElementById("st_3").checked;
    var c4 = document.getElementById("st_4").checked;
    var values = "";
    if(c1){
        //first check box is checked. you can add your desired code here
        values = values + "1";
    }
    if(c1 && c2){
        values = values + ",2";
    }
    if(c1 && c2 && c3){
        values = values + ",3";
    }
    if(c1 && c2 && c3 && c4){
        values = values + ",4";
    }
    alert(values);
}

编辑:使用jquery:

function checkTest(){
    // script and condition here that i'm stuck to begin
    var c1 = $("#st_1").prop("checked");
    var c2 = $("#st_2").prop("checked");
    var c3 = $("#st_3").prop("checked");
    var c4 = $("#st_4").prop("checked");
    var values = "";
    if(c1){
        //first check box is checked. you can add your desired code here
        values = values + "1";
    }
    if(c1 && c2){
        values = values + ",2";
    }
    if(c1 && c2 && c3){
        values = values + ",3";
    }
    if(c1 && c2 && c3 && c4){
        values = values + ",4";
    }
    alert(values);
}