首页 > 解决方案 > 如何保存超过 3 个 html 的数据

问题描述

您好,我有一个带有多个 html/input 标签的表单。像这样:

   <form class="form-group">
            <div class="body table-responsive">
                <table class="table m-b-0">
                    <thead>
                        <tr>

                            <th>Name</th>
                            <th>age</th>
                            <th>phone number</th>
                            <th>ID</th>

                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>

                                <td>@item.name</td>
                                <td>
                                    <input type="text" name="" id="" value="" 
                                    class="bordered">
                                </td>
                                <td>
                                    <input type="text" name="" id="" value="" 
                                   class="bordered">
                                </td>
                                <td>
                                    <input type="text" name="" id="" value="" 
                                    class="bordered">
                                </td>


                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </form>

我还没有命名或标识它们,因为我知道有一种方法可以填充所有输入并在按下提交或按钮时同时保存它们,我只是不知道如何。

我是否需要在 上添加标签或属性以便能够在按下时同时保存所有输入?

标签: c#htmlvisual-studio-2017

解决方案


您必须为每个输入分配名称并添加一个提交按钮。当您单击提交时,用户输入将使用给定的方法发送到服务器。

<form method="post" class="form-group">
    <div class="body table-responsive">
        <table class="table m-b-0">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>age</th>
                    <th>phone number</th>
                    <th>ID</th>
                </tr>
            </thead>
            <tbody>
                @{
                    var i = 0;
                    foreach (var item in Model) {
                    <tr>
                        <td>@item.name</td>
                        <td>
                            <input type="text" name="input1_@i" id="" value="" 
                            class="bordered">
                        </td>
                        <td>
                            <input type="text" name="input2_@i" id="" value="" 
                           class="bordered">
                        </td>
                        <td>
                            <input type="text" name="input3_@i" id="" value="" 
                            class="bordered">
                        </td>
                    </tr>
                    }
                }
            </tbody>
        </table>
    </div>
    <button type="submit">Submit</button>
</form>

或者你可以监听提交事件并自己处理。

$('form').submit(function(e) {
    e.preventDefault();
    // you can get the input data from 'this' (form element)
    // here comes your validation and server call
});

推荐阅读