2013-06-01 28 views
0

我有一個表格有多個相同的文件,例如: 數量零件編號製造價格和低於這些將是5行,我想將其減少到1行,並給予用戶添加額外行的選項多列,如果他們需要。這是必須用JavaScript來完成的嗎?我可以像一個小按鈕,點擊這裏添加一個額外的行?如何在php中創建一個允許用戶在需要時添加其他表單域的表單?

我試過,但它不添加任何字段:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
     <link href="/style.css" rel="stylesheet" type="text/css" /> 
     <title>More form fields</title> 
     <script type="text/javascript"> 
      FUNCTION addRowToTable() 
      { 
      VAR tbl = document.getElementById('tblAddress'); 
        VAR lastRow = tbl.rows.length; 
        // if there's no header row in the table, then iteration = lastRow + 1 
        VAR iteration = lastRow; 
        // var iteration = lastRow + 1; 
        VAR row = tbl.insertRow(lastRow); 
        // cell 0 
        VAR cell0 = row.insertCell(0); 
        VAR el = document.createElement('input'); 
        el.type = 'text'; 
        el.NAME = 'Address[]'; 
        el.size = 30; 
        cell0.appendChild(el); 
        //cell 1 
        VAR cell1 = row.insertCell(1); 
        VAR el = document.createElement('input'); 
        el.type = 'text'; 
        el.NAME = 'City[]'; 
        el.size = 10; 
        cell1.appendChild(el); 
        //cell 2 
        VAR cell2 = row.insertCell(2); 
        VAR el = document.createElement('input'); 
        el.type = 'text'; 
        el.NAME = 'State[]'; 
        el.size = 2; 
        cell2.appendChild(el); 
        //cell 3 
        VAR cell3 = row.insertCell(3); 
        VAR el = document.createElement('input'); 
        el.type = 'text'; 
        el.NAME = 'Zip[]'; 
        el.size = 5; 
        cell3.appendChild(el); 
      } 
     </script> 
    </head> 

    <body> 
     <h3>Dynamic add form fields</h3> 

     <form action="Untitled-2.php" name="h" method="post"> 
      <table id="tblAddress"> 
       <tr> 
        <td class="txtBase">Address</td> 
        <td class="txtBase">City</td> 
        <td class="txtBase">State</td> 
        <td class="txtBase">Zip</td> 
       </tr> 
       <tr> 
        <td><input name="Address[]" type="text" size="30" maxlength="255"></td> 
        <td><input name="City[]" type="text" size="10" maxlength="255"></td> 
        <td><input name="State[]" type="text" size="2" maxlength="10"></td> 
        <td><input name="Zip[]" type="text" size="5" maxlength="25"></td> 
       </tr> 
      </table> 
      <input type="button" name="Add" value="Add" onClick="addRowToTable();"/> 
      <input type="submit" name="Submit" value="Submit" /> 
     </form> 
    </body> 
</html> 
+0

使用JS添加更多行,然後設置您的PHP以容納X行處理。 –

+0

是的,爲了保持簡單,你必須使用js來完成它。只需添加一個函數,該函數將根據指定的數字動態創建元素,並將其與按鈕單擊事件相關聯。請確保爲每個元素分配了一個不同的ID,以便它們可以很容易地分別處理它們 – dreamweiver

回答

2

下面是使用jQuery> 1.7,略低於詳細的版本,它的首選()方法的事件綁定:

http://jsfiddle.net/tZPg4/5077/

<form> 
    <input type="text" /> 
</form> 
<button id="addFields">Add another field</button> 

<script> 

$(function ($) { 

    $('body').on("click", '#addFields', function() { 
     $('form').append('<input type="text" />'); 
    }) 

})(jQuery); 

</script> 
+0

也許我做錯了什麼,但當我點擊按鈕時,它不會添加另一個字段,與矢量腳本以及我在我的問題中發佈的腳本一樣。 – Jason

+0

您的Javascript已禁用嗎? –

+0

另外,如果您將其複製到您的項目中,則需要在HEAD中添加

0

JavaScript或PHP你可以做到這一點。

Javascript在各方面都更快更好,每次需要添加字段時都不會觸及服務器。

當你需要添加一個字段並在服務器端完成時,PHP會要求刷新頁面,這會增加服務器的負載。每次需要添加或刪除字段時,只需重新構建表單即可。不要忘記重新填充已填充的表單域。

0

使用jQuery你可以做到這一點...

<div id="pricecontainer"></div> 
<a id="addprice" href="javascript::void(0)">Add More</a> 


<script> 
$('#addprice').click(function(){ 
      $len= $("#pricecontainer").children("div").length; 
      $("#pricecontainer").append("<div class='FormWrap PriceBorder' id='"+$len+"'><div class='input text'><label>Price</label><input style='width: 100px;' class='price' type='text' name='price[]' id='price"+$len+"' maxlength='5'></div></div>"); 

     }); 
     </script> 
+0

$ len = $(「#pricecontainer」)。children(「div」)。length;它會計數價格容器中附加的div的孩子。 – Pank

相關問題