0

我在从动态添加的 jquery 表行中检索输入值时遇到了一些问题......这些是我的代码..期待一个好的解决方案..提前谢谢:)

    <head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 //This line clones the row inside the '.row' class and transforms it to plain html.
 var clonedRow = $('.row').clone().html();

 //This line wraps the clonedRow and wraps it <tr> tags since cloning ignores those tags
 var appendRow = '<tr class = "row">' + clonedRow + '</tr>';  

 $('#btnAddMore').click(function(){
  //this line get's the last row and appends the appendRow when it finds the correct row.
        $('.employmentHistoryForm tr:last').after(appendRow);
    });

 //when you click on the button called "delete", the function inside will be triggered.
 $('.deleteThisRow').live('click',function(){
     var rowLength = $('.row').length;
         //this line makes sure that we don't ever run out of rows.
      if(rowLength > 1){
   deleteRow(this);
  }else{
   $('.employmentHistoryForm tr:last').after(appendRow);
   deleteRow(this);
  }
 });

 function deleteRow(currentNode){
  $(currentNode).parent().parent().remove();
 }
 });
 </script> 
</head>

// 最后我需要从文本字段传递值...

<body>
<form name="add_row" method="post" action="process.php">
<div class="employmentHistory">
<table class="employmentHistoryForm">
 <tr class = "row">
  <td> <label for="company">Name</label></br>
  <input type="text" name="name"  />



  <g:textField name="company" class="company">

  </g:textField></td>
     <td><label for="position"> Position </label></br>
     <input type="text" name="name"  />
  <g:textField name="position" ></g:textField></td>
  <td></br><input type="button" class="deleteThisRow"  value="Delete"/></td>
 </tr>
</table>
</div>
<input type="button" id="btnAddMore"  value="add more"/>
</body>
4

2 回答 2

1

将文本字段名称更改为name1,name2,name3....namen

在 php 端获取数组的长度。

$numoftxtfield = count($_POST);

这给出了文本字段的长度,然后使用 for 循环或 foreach 循环获取文本字段的所有值。

于 2013-10-24T06:54:21.640 回答
1

如果您不想更改文本字段的名称,那么您可以将名称用作数组,例如.. name = "name[]" 在提交表单后,您可以通过 implode/explode 检索它。

于 2013-10-24T07:54:18.353 回答