【问题标题】:Generate dynamic input fields based on selection根据选择生成动态输入字段
【发布时间】:2015-02-27 17:57:04
【问题描述】:

我需要一个表格,该表格应该可以选择不选择成员。根据数字,用户选择为输入生成的表格行数。 此刻我有以下一个..它有选择..但是定义了最多四行字段。用户只需选择成员编号,然后填写表格。 我希望该用户首先选择没有成员。例如用户选择 2 ...然后将生成 2 行来填充。

 <div class="form-group">
      <label>
        No Of Members: <select name="member" class="form-control" required>
          <option value="0">Select</option>
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
          <option value="4">4</option>


       </select>
    </label>
  </div>

 <div>           
          <table class="table table-bordered">
                <thead>
                    <tr class="active">
                        <th>Student ID</th>
                        <th>Name</th>
                        <th>Email </th>
                        <th>CGPA</th>

                    </tr>
                </thead>
                <tbody>
                      <tr>
                        <td><input type="text" name="studentid" class="form-control" id="" placeholder="Enter Student ID" required></td>
                        <td><input type="text" name="name" class="form-control" id="" placeholder="Enter Name" required></td>
                        <td><input type="text" name="email" class="form-control" id="" placeholder="Enter Email" required></td>
                        <td><input type="text" name="cgpa" class="form-control" id="" placeholder="Enter CGPA" required></td>
                    <tr/>
                    <tr>
                        <td><input type="text" name="studentid" class="form-control" id="" placeholder="Enter Student ID"></td>
                        <td><input type="text" name="name" class="form-control" id="" placeholder="Enter Name"></td>
                        <td><input type="text" name="email" class="form-control" id="" placeholder="Enter Email"></td>
                        <td><input type="text" name="cgpa" class="form-control" id="" placeholder="Enter CGPA"></td>
                    <tr/>
                    <tr>
                        <td><input type="text" name="studentid" class="form-control" id="" placeholder="Enter Student ID"></td>
                        <td><input type="text" name="name" class="form-control" id="" placeholder="Enter Name"></td>
                        <td><input type="text" name="email" class="form-control" id="" placeholder="Enter Email"></td>
                        <td><input type="text" name="cgpa" class="form-control" id="" placeholder="Enter CGPA"></td>
                    <tr/>
                    <tr>
                        <td><input type="text" name="studentid" class="form-control" id="" placeholder="Enter Student ID"></td>
                        <td><input type="text" name="name" class="form-control" id="" placeholder="Enter Name"></td>
                        <td><input type="text" name="email" class="form-control" id="" placeholder="Enter Email"></td>
                        <td><input type="text" name="cgpa" class="form-control" id="" placeholder="Enter CGPA"></td>
                    <tr/>


                </tbody>
            </table>
        </div>

我在 JSFiddle [http://jsfiddle.net/sushanth009/H3z5P/] 中找到了类似的解决方案。 但它有一个问题......每次我输入一个数字......它都会将之前的行数相加。它没有选择。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您应该在问题中包含 Fiddle 中的关键代码。

    如果我了解您想要什么,您似乎需要根据表的大小和请求的行数来增加或减少行数。因此您可以测试当前的数字并进行如下调整:

    $('[name="cand_no"]').on('change', function() {
      // Not checking for Invalid input
      if (this.value != '') {
        var val = parseInt(this.value, 10);
        var rows = $('#studentTable tr'),
            rowCnt = rows.length - 1; // Allow for header row
    
        if (rowCnt > val) {
            for (var i = rowCnt; i > val; i--) {
                rows[i].remove();
            }
        }
        if (rowCnt < val) {
            for (var i = 0; i < val - rowCnt; i++) {
                // Clone the Template
                var $cloned = $('.template tbody').clone();
                // For each Candidate append the template row
                $('#studentTable tbody').append($cloned.html());
            }
        }
      }
    });
    

    见Fiddle

    【讨论】:

    • 是的......就像这样......但只需要选择而不是在候选人人数中输入
    • 把这个$('[name="cand_no"]')改成$('select [name="member"]')
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多