簡體   English   中英

獲取復選框的值

[英]getting the value of checked checkbox

我想獲取選中的復選框的值,但問題是它們在我的網頁上將是動態的,例如:

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>";
}

ajax調用getCheckBok()將從服務器獲取結果,但在checkBoxArea上,結果將如下所示:

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

當按下按鈕w時,我需要獲取已選中復選框的值,我該怎么做?

我已經嘗試過這個jQuery代碼:

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

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