2013-10-24 56 views
0

我有一些問題,從動態添加jQuery的錶行檢索輸入值... 這些都是我的代碼..期待一個很好的解決方案。在此先感謝:)如何從動態添加的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> 

// finaly我需要從文本費爾德值傳遞...

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

回答

1

更改文本字段名稱name1,name2,name3....namen

在PHP端得到​​數組的長度等。

$numoftxtfield = count($_POST); 

這給然後使用循環或foreach循環獲取文本字段的所有值的文本字段的長度。

1

如果您不想更改文本字段的名稱,那麼您可以使用名稱作爲數組,例如.. name =「name []」 提交表單後,您可以通過implode/explode來檢索它。