简体   繁体   中英

How to get datatable input field checkbox checked value

I have an input field checkbox in datatable. How can I get values of the checked checkbox?

<form method="post"><tr>
<td ></td>
<td ><input type="checkbox" name="multicheck[]" class="multicheck" 
 value='.$value['id'].' /> </td>
</tr>
</form>

I have not copied the entire table above.

Thanks.

You could do a selector like this:

Get all checked elements: document.querySelectorAll('input[type="checkbox"]:checked')

Than you could transform the nodelist to an array and map to extract only the values:

const checkedValues = [
    ...document.querySelectorAll('input[type="checkbox"]:checked')
].map(check => check.value);

Example:

 document.querySelector("#btn").addEventListener("click", () => { const checkedValues = [ ...document.querySelectorAll('input[type="checkbox"]:checked') ].map(check => check.value); console.log("checked values", checkedValues); });
 <input id="xyz" type="checkbox" name="multicheck[]" class="multicheck" value="xyz" /> <label for="xyz">xyz</label> <input id="abc" type="checkbox" name="multicheck[]" class="multicheck" value="abc" /> <label for="abc">abc</label> <button id="btn">get checked values</button>

Small demo https://codesandbox.io/s/checkbox-demo-oeiu5

Hope this works

const values = [];
const inputs = document.querySelectorAll('input.multicheck');

for(let i=0; i<inputs.length; i++){
   if( inputs[i].checked ){
      values.push( inputs[i].value );
    }
}

console.log(values)

Then variable values will have the values of the checked checkboxs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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