簡體   English   中英

如何在按鈕單擊時添加動態文本字段

[英]how do add dynamic textfield on button click

我正在處理 jsp pages.Here 我有在按鈕單擊時添加文本字段的要求。

如圖所示。

我無法編寫 java 腳本函數來在按鈕單擊時添加 textField。

 <body>
 <button type="button">Add more !</button>
 <table id="customers">
   <tr>
     <th>Product</th>
     <th>Quantity</th>
     <th>Price</th>
  </tr>
      <tr>
         <td><input type="text" name="product" value="product.."></input></td>
        <td><input type="number" name="quantity" value="quanty.."></input></td>
        <td><input type="number" name="price" value="price.."></input></td>
      </tr>
    // i want to add these fields dynamically on button click.
 </table>
</body>

這是一個純js的基本例子

 <p>Create Text Field.</p> <button onclick="createFunction()">create it</button> <script> function createFunction() { var x = document.createElement("INPUT"); x.setAttribute("type", "text"); x.setAttribute("value", "Hello Pritam !"); document.body.appendChild(x); } </script>

您可以使用 .clone() 方法獲取現有行的克隆(副本)。 然后你可以 append() 新行。,

$("#add").click(function() {
  var table = $("#customers");
  table.append(table.find("tr:eq(1)").clone());

});

小提琴

這是如何使用 jQuery 在頁面中添加 HTML 的示例。

$("BUTTON").click(function(){
    $("#customers TR").append('<td><input type="yourtype" name="yourname" value="yourvalue"></input></td>');
});

在這里閱讀更多。

你試一試。 只是一個例子。

    <html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>

<button type="button">Add more !</button>
 <table id="customers">
   <tr>
     <th>Product</th>
     <th>Quantity</th>
     <th>Price</th>
  </tr>
      <tr>
         <td><input type="text" name="product" value="product.."></input></td>
        <td><input type="number" name="quantity" value="quanty.."></input></td>
        <td><input type="number" name="price" value="price.."></input></td>
      </tr>

 </table>

  <script>

     $(document).ready(function(){

       $("button").on("click", function(){

         var row = '<tr><td><input type="text" name="product" value="product.."></input></td><td><input type="number" name="quantity" value="quanty.."></input></td><td><input type="number" name="price" value="price.."></input></td></tr>';

         $("#customers").append(row);

       });

     });

    </script>

    </body>
    </html>

網址:

<table id="customers">
   <tr>
     <th>Product</th>
     <th>Quantity</th>
     <th>Price</th>
  </tr>
  <tr>
    <td><input type="text" name="product" value="product.."></input></td>
    <td><input type="number" name="quantity" value="quanty.."></input></td>
    <td><input type="number" name="price" value="price.."></input></td>
  </tr>
 </table>

查詢:

i = 1; // yoi can assign unique name to each textbox using this i
$( document ).ready(function() {

  $("#add").click(function() {
     $("#customers").append('<tr><td><input type="text" name="product'+i+'" value="product.."></input></td> <td><input type="number" name="quantity'+i+'" value="quanty.."></input></td>     <td><input type="number" name="price'+i+'" value="price.."></input></td> </tr>');
     i++;

  })

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM