简体   繁体   中英

How to perform operations on selected rows in checkbox in a table in HTML

I have a table and I want to perform some operations(suppose doing output of selected rows in alert) on the rows which the user have checked on. Following is my approach which is failing miserably-

 <html> <head> <title>Introduction</title> <script> function kro(){ var x = document.getElementsByName('checks'); for (var i = 0;i < x.length;i++){ if(x[i].checked == 1) var selecteddata+=x[i].value+"\\n"; } alert(selecteddata) } </script> </head> <body> <table> <thead> <tr> <th>UserName</th> <th>Post</th> <th>Comments</th> <th>Select</th> </tr> </thead> <tbody> <tr> <td>Ram</td> <td>Sahi hai</td> <td>Ravan:No</td> <td><input type = "checkbox" name = "checks" id='one'/>&nbsp;</td> </tr> <tr> <td>Ravan</td> <td>Kidnap done</td> <td>Ram:I'll kill you</td> <td><input type = "checkbox" name = "checks" id='two'/>&nbsp;</td> </tbody> <p id = "check" ></p> <button onclick="kro()"> Result </button> </body> </html>

Following is my table- 在此处输入图片说明

I want to perform further operations on the selected row ,in the code ,I just want to output the selected rows using alert. How to do that?

Expected Output-

if first row is checked then

Ram Sahi hai    Ravan:No

var selecteddata+=x[i].value+"\\n"; line will throw an error, So declare the variable outside the for loop. Also you need to set a value to checkbox otherwise it will so on

To get the content from all the td get the parent of the checkbox and get the closest tr . Then get text from each of the td and add it in a local variable of the if block

 function kro() { var x = document.getElementsByName('checks'); var selecteddata = ''; for (var i = 0; i < x.length; i++) { if (x[i].checked) { let _tt = ''; x[i].closest('tr').querySelectorAll('td').forEach(function(item) { _tt += item.textContent.trim(); }) selecteddata += _tt + "\\n"; } } alert(selecteddata) }
 <table> <thead> <tr> <th>UserName</th> <th>Post</th> <th>Comments</th> <th>Select</th> </tr> </thead> <tbody> <tr> <td>Ram</td> <td>Sahi hai</td> <td>Ravan:No</td> <td><input type="checkbox" name="checks" id='one' value='test1' />&nbsp;</td> </tr> <tr> <td>Ravan</td> <td>Kidnap done</td> <td>Ram:I'll kill you</td> <td><input type="checkbox" name="checks" id='two' value='test2' />&nbsp;</td> </tbody> <p id="check"></p> <button onclick="kro()"> Result </button>

You define and concat to the variable wrong.

should be

 var foo = ''; // declare outside of loop for (var i=0; i<5; i++) { foo += i + '\\n' // concat to string } console.log(foo) //display it

Other way people would do it is with an array and joining it at the end

 var foo = []; // declare outside of loop for (var i=0; i<5; i++) { foo.push(i) // add to array } console.log(foo.join('\\n')) //display it by joining the array items

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