如何从动态添加的jquery行获取文本字段值?

时间:2013-10-24 06:45:41

标签: php jquery dynamic

我从动态添加的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>

//最后我需要传递来自text feild的值......

<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>

2 个答案:

答案 0 :(得分:1)

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

php端的

获取数组的长度。

$numoftxtfield = count($_POST);

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

答案 1 :(得分:1)

如果您不想更改文本字段的名称,则可以使用name作为数组...      name =“name []” 提交表格后,你可以通过implode / explode来回复它。