简体   繁体   English

如何使用jQuery检索名称列表属性(html)中的数据?

[英]How do I retrieve the data in the name list attribute (html) using jQuery?

I am trying to retrieve the list of names from the inputs through jQuery. 我正在尝试通过jQuery从输入中检索名称列表。 Could anyone advise how I can do it? 谁能建议我该怎么做?

The following inputs for example: 例如以下输入:

Henry | 25 | Financial Advisor
Eric  | 28 | Chemistry Professor
Lance | 30 | Database Administrator

How can I retrieve the names Henry, Eric, Lance using jQuery and the name[ ] attribute? 如何使用jQuery和name []属性检索名称Henry,Eric,Lance?

HTML 的HTML

<div id="wrapper">
<div id="form_div">
 <form method="post" action="store_employee.php">
  <table id="employee_table" align=center>
   <tr id="row1">
    <td><input type="text" name="name[]" placeholder="Enter Name"></td>
    <td><input type="text" name="age[]" placeholder="Enter Age"></td>
    <td><input type="text" name="job[]" placeholder="Enter Job"></td>
   </tr>
  </table>
  <input type="button" onclick="add_row();" value="ADD ROW">
  <input type="submit" name="submit_row" value="SUBMIT">
 </form>
</div>

Javascript Java脚本

    function add_row()
{
    $rowno=jQuery("#employee_table tr").length;
    $rowno=$rowno+1;
    jQuery("#employee_table tr:last").after(
        "<tr id='row"+$rowno+"'>"+
            "<td><input type='text' name='name[]' placeholder='Enter Name'></td>"+
            "<td><input type='text' name='age[]' placeholder='Enter Age'></td>"+
            "<td><input type='text' name='job[]' placeholder='Enter Job'></td>"+
            "<td><input type='button' value='DELETE' onclick=delete_row('row"+$rowno+"')></td>"+
        "</tr>");
    }


function delete_row(rowno)
{
    jQuery('#'+rowno).remove();
}

You can use map() 您可以使用map()

 let res = $('tr').map((_, row) => { return [$(row).find('input').map((_, el) => el.value).get()] }).get() console.log(res) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <div id="form_div"> <form method="post" action="store_employee.php"> <table id="employee_table" align=center> <tr id="row1"> <td><input type="text" name="name[]" placeholder="Enter Name" value="Joe"></td> <td><input type="text" name="age[]" placeholder="Enter Age" value="34"></td> <td><input type="text" name="job[]" placeholder="Enter Job" value="President"></td> </tr> <tr id="row1"> <td><input type="text" name="name[]" placeholder="Enter Name" value="Bill"></td> <td><input type="text" name="age[]" placeholder="Enter Age" value="77"></td> <td><input type="text" name="job[]" placeholder="Enter Job" value="Flunky"></td> </tr> </table> <input type="button" onclick="add_row();" value="ADD ROW"> <input type="submit" name="submit_row" value="SUBMIT"> </form> </div> 

You can use the name attribute to target the right inputs... Then look for the value. 您可以使用name属性来定位正确的输入...然后寻找值。

 function add_row() { $rowno=jQuery("#employee_table tr").length; $rowno=$rowno+1; jQuery("#employee_table tr:last").after( "<tr id='row"+$rowno+"'>"+ "<td><input type='text' name='name[]' placeholder='Enter Name'></td>"+ "<td><input type='text' name='age[]' placeholder='Enter Age'></td>"+ "<td><input type='text' name='job[]' placeholder='Enter Job'></td>"+ "<td><input type='button' value='DELETE' onclick=delete_row('row"+$rowno+"')></td>"+ "</tr>"); } function delete_row(rowno) { jQuery('#'+rowno).remove(); } $("#test").on("click",function(){ var names =[]; var nameInputs = $("input[name='name[]']"); for (i=0;i<nameInputs.length;i++){ names.push(nameInputs.eq(i).val()); } console.log(names.join(",")); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <div id="form_div"> <form method="post" action="store_employee.php"> <table id="employee_table" align=center> <tr id="row1"> <td><input type="text" name="name[]" placeholder="Enter Name"></td> <td><input type="text" name="age[]" placeholder="Enter Age"></td> <td><input type="text" name="job[]" placeholder="Enter Job"></td> </tr> </table> <input type="button" onclick="add_row();" value="ADD ROW"> <input type="submit" name="submit_row" value="SUBMIT"> </form> </div> <br> <br> <button id="test">Show names in console</button> 

var names = $("[name='name[]']");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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