亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在輸入表中添加或刪除行

在輸入表中添加或刪除行

紫衣仙女 2024-01-18 10:41:43
我正在嘗試設計一個帶有輸入表的網頁,用戶可以根據需要添加或刪除行。用戶界面是這樣的——這是我的html代碼:<div class="container my-5">  <h2>Welcome to dynamic input table with row adding option</h2>  <form method="" action="">    <table class="table table-hover my-5">        <thead class="">            <tr>                <th>No</th>                <th>Name</th>                <th>Pnone Number</th>                <th>Email</th>                <th>Remove?</th>            </tr>        </thead>        <tbody>                   <tr>            <td>1</td>            <td><input type="text" name="name-1"></td>            <td><input type="text" name="phone-1"></td>            <td><input type="text" name="Email-1"></td>            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>          </tr>          <tr>            <td>2</td>            <td><input type="text" name="name-2"></td>            <td><input type="text" name="phone-2"></td>            <td><input type="text" name="Email-2"></td>            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>          </tr>          <tr>            <td>3</td>            <td><input type="text" name="name-3"></td>            <td><input type="text" name="phone-3"></td>            <td><input type="text" name="Email-3"></td>            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>          </tr>          <tr>            <td>4</td>            <td><input type="text" name="name-4"></td>            <td><input type="text" name="phone-4"></td>            <td><input type="text" name="Email-4"></td>            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>          </tr>                     </tbody></head>現在我的問題是如何實現按鈕Add row、Delete last row按鈕和remove. 一切都應該以這樣的方式工作,我也可以使用它在后端發送數據。我更喜歡使用Django并MongoDB實現我的后端?,F在請幫助我實現這個的最佳方法,如果它可以在前端用js實現,那對我來說將非常有幫助,或者如果它應該在后端用動態方法實現,它也將起作用。
查看完整描述

2 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

您的問題有很多部分,應該分為幾個問題。這是刪除行的簡單方法。您需要制作一個可單擊的按鈕,而不是我在這里所做的。而且,您還需要進行 ajax 調用來刪除數據庫中的實際數據。這只是前端代碼,用于直觀地向用戶顯示一行已被刪除。


$('.my-5 tr').click(function(){

    $(this).remove();

    return false;

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container my-5">

  <h2>Welcome to dynamic input table with row adding option</h2>

  <form method="" action="">

    <table class="table table-hover my-5">


        <thead class="">

            <tr>

                <th>No</th>

                <th>Name</th>

                <th>Pnone Number</th>

                <th>Email</th>

                <th>Remove?</th>

            </tr>

        </thead>


        <tbody>         

          <tr>

            <td>1</td>

            <td><input type="text" name="name-1"></td>

            <td><input type="text" name="phone-1"></td>

            <td><input type="text" name="Email-1"></td>

            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>

          </tr>

          <tr>

            <td>2</td>

            <td><input type="text" name="name-2"></td>

            <td><input type="text" name="phone-2"></td>

            <td><input type="text" name="Email-2"></td>

            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>

          </tr>

          <tr>

            <td>3</td>

            <td><input type="text" name="name-3"></td>

            <td><input type="text" name="phone-3"></td>

            <td><input type="text" name="Email-3"></td>

            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>

          </tr>

          <tr>

            <td>4</td>

            <td><input type="text" name="name-4"></td>

            <td><input type="text" name="phone-4"></td>

            <td><input type="text" name="Email-4"></td>

            <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>

          </tr>             

        </tbody>


      </table>

      <div class="row m-0">

        <button class="btn btn-warning">Add row</button>

        <button class="btn btn-danger ml-3">Delete last row</button>

        <button type="Submit" class="btn btn-primary ml-auto">Submit</button>

      </div>  

  </form>  

</div>


查看完整回答
反對 回復 2024-01-18
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

如果您想要執行添加和刪除等操作,您可以動態創建表。

您可以在此處查看演示: https://jsbin.com/pewexibole/edit? html,js,console,output

HTML:

<head>

    <title></title>


    <!-- media query support -->

    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />


    <!-- Latest compiled and minified CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">


    <!-- jQuery library -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


    <!-- Popper JS -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>


    <!-- Latest compiled JavaScript -->

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>


    <!-- font awsome css link -->   

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


</head>


<div class="container my-5">

  <h2>Welcome to dynamic input table with row adding option</h2>

    <table class="table table-hover my-5">


        <thead class="">

            <tr>

                <th>No</th>

                <th>Name</th>

                <th>Pnone Number</th>

                <th>Email</th>

                <th>Remove?</th>

            </tr>

        </thead>


        <tbody>        

        </tbody>


      </table>

      <div class="row m-0">

        <button class="btn btn-warning" onclick="addRow()">Add row</button>

        <button class="btn btn-danger ml-3">Delete last row</button>

        <button type="Submit" class="btn btn-primary ml-auto">Submit</button>

      </div>

</div>

JS:


let i = 0;


function rowTemplate(i) {

  return `<tr data-index=${i}>

      <td>${i}</td>

      <td><input type="text" name="name-${i}"></td>

      <td><input type="text" name="phone-${i}"></td>

      <td><input type="text" name="Email-${i}"></td>

      <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;" onclick="removeRow(${i})"></i></td>

    </tr>`

}


for (i = 0; i < 4; i ++) {

  $('tbody').append(rowTemplate(i));

}


function removeRow(i) {

  $("tbody").find(`tr[data-index='${i}']`).remove();

}


function addRow() {

  $('tbody').append(rowTemplate(i));

  i++;

}


查看完整回答
反對 回復 2024-01-18
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號