簡體   English   中英

如何從jQuery的“警報”框中的選中行的文本框中獲取數據?

[英]How to get the data from text boxes of checked rows on Alert box in Jquery?

我想查看每當我按下按鈕時在m警報框中選中的所有行,但是我在這里做錯了

用於添加行的JavaScript

 $("#add").click(function () {
    $("#myTable").last().append("<tr><td width='5%'><input type='checkbox'>                                       
   </td><td><input type='text' id='1stid'>  
  </td><td><input type='text' id='2ndid'></td><td><input type='text'id='3rdid'></td></tr>");
 });

我也這樣做了,但現在警報框為空

$("#view").click(function () {
  $('input[type="checkbox"]:checked');
    var n=$(this).closest('tr').text();
    alert(n);
});

我的HTML

 <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <link rel="stylesheet" href="Project.css"> </head> <body> <div class="container"> <button id="add">Add</button> <button id="deleteLast">Remove </button> <button id="delete"> Remove All</button> <button id="view">View</button> <table class="table table-bordered" id="myTable"> <tr> <th width="5%"></th> <th>First name</th> <th>Last name</th> <th>Profession</th> </tr> </table> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script type="text/javascript" src="Project.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html> 

由於$("input[type='checkbox']:checked")將返回元素的數組/列表,因此請使用jQuery的each方法$("input[type='checkbox']:checked").each(...)遍歷結果

 $("#view").click(function() { $("input[type='checkbox']:checked").each(function(index) { $('td input[type=text]', $(this).closest('tr')).each(function(index2) { alert( $(this).val() ); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr class="test"> <td width='5%'> <input type='checkbox'> </td> <td> <input type='text' id='11stid' value="1 first"> </td> <td> <input type='text' id='12ndid' value="1 second"> </td> <td> <input type='text' id='13rdid' value="1 third"> </td> </tr> <tr> <td width='5%'> <input type='checkbox'> </td> <td> <input type='text' id='21stid' value="2 first"> </td> <td> <input type='text' id='22ndid' value="2 second"> </td> <td> <input type='text' id='23rdid' value="2 third"> </td> </tr> <tr> <td width='5%'> <input type='checkbox'> </td> <td> <input type='text' id='31stid' value="3 first"> </td> <td> <input type='text' id='32ndid' value="3 second"> </td> <td> <input type='text' id='33rdid' value="3 third"> </td> </tr> </table> <button id="view">Checked</button> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM