简体   繁体   中英

show alert if checked checkbox with different attr in javascript

In my project, I need to check if the data-id-row values are equal to the data-id-row values of the checked table rows when the checkboxes are checked, return a value if it is equal, and a message if it is not equal. Show it.

this is my rows

 <table> <thead> <tr> <th> <input type="checkbox" name="id[]"> </th> <th> first name </th> <th> last name </th> </tr> </thead> <tbody> <tr id="6"> <td><input type="checkbox"></td> <td>hamid</td> <td>hamidi</td> </tr> <tr id="7"> <td><input type="checkbox"></td> <td>john</td> <td>tyler</td> </tr> <tr id="8"> <td><input type="checkbox"></td> <td>john</td> <td>smith</td> </tr> </table>

How can I do that?

Try this, it will show you id's of all checked rows:

var trs= document.getElementsByTagName("tr");

for (var i = 0; i < trs.length; i++){
var tr = trs[i];
var checks = tr.getElementsByTagName("input");
if (checks.length > 0 && checks[0].checked)
     alert(tr.id)
}

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