简体   繁体   English

获取复选框的值

[英]getting the value of checked checkbox

I am trying to get the value of checked checkBoxes but the problem is that they will be dynamic on my web page eg : 我想获取选中的复选框的值,但问题是它们在我的网页上将是动态的,例如:

while ($row=myqli_fetch_array($result)){
    echo"<div>";
    echo"<select id=\"course\" onchange=getCheckBox()>
    <option>1</option>
    <option>2</option>
    </select>";
    echo"<div id=\"checkBoxArea\">";
    echo"<buttton id=\"w\" >w</button></div>";
}

the ajax call getCheckBok() will get the result from a server and but it on checkBoxArea the result will look like this : ajax调用getCheckBok()将从服务器获取结果,但在checkBoxArea上,结果将如下所示:

echo "<input type=\"checkbox\" value=\"aaa\" class=\"stdcheckbox\">";
echo "<input type=\"checkbox\" value=\"bbb\" class=\"stdcheckbox\">";

when pressing on button w I need to get the value of the checked checkbox how can I do this ? 当按下按钮w时,我需要获取已选中复选框的值,我该怎么做?

I have tried this jQuery code : 我已经尝试过这个jQuery代码:

var x=$(this).parent().find(".stdCheckBox").value;

Vanilla JavaScript solution to create an Array of the values of checked <input type="checkbox"/> s Vanilla JavaScript解决方案,用于创建已检查 <input type="checkbox"/>数组

function checked_values(node) {
    var checked;
    if (!node) node = document;
    checked = node.querySelectorAll('input[type="checkbox"]:checked');
    return Array.prototype.map.call(checked, function (e) {return e.value;});
}

  function checked_values(node) { var checked; if (!node) node = document; checked = node.querySelectorAll('input[type="checkbox"]:checked'); return Array.prototype.map.call(checked, function (e) {return e.value;}); } window.addEventListener('load', function () { document.querySelector('button').addEventListener('click', function () { console.log(checked_values()); }); }); 
 <label><input type="checkbox" value="a"/>a</label><br/> <label><input type="checkbox" value="b"/>b</label><br/> <label><input type="checkbox" value="c"/>c</label><br/> <label><input type="checkbox" value="d"/>d</label><br/> <button>Test (console)</button> 

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

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