Serialise HTML Form with time/text input type and validation of time fields

题意:将具有时间和文本输入类型的HTML表单序列化,并对时间字段进行验证

问题背景:

I am trying to create an HTML Form that will be used to send data to the backend in C#.

我正在尝试创建一个HTML表单,该表单将用于将数据发送到C#编写的后端。

Below is the Form which consists of 2 fields: FROMTIME and TOTIME.

下面是一个表单,包含两个字段:FROMTIME 和 TOTIME

I need to make sure that the user inputs the values in the format of HH:MM but this is not feasible without validation.

我需要确保用户以HH:MM的格式输入值,但没有验证这是不可行的。

I have tried using input type="time" but this does not allow me to serialise the data and send that to the backend.

我尝试过使用input type="time",但这不允许我序列化数据并将其发送到后端。

I am using $("#editForm").serialize or $("#editForm").serializeObject or $("#editForm").serializeArray but the data is empty because the serialisation does not include the Time data.

我正在使用$("#editForm").serialize()$("#editForm").serializeObject()$("#editForm").serializeArray(),但数据是空的,因为序列化不包括时间数据

Any ideas please?

Is there a way to force the user to input hour and minutes in the format of HH:MM?

有一种方法可以强制用户以HH:MM的格式输入小时和分钟吗?

<form id="editForm">
  <table>
    <tr>
      <th>From Time: </th>
      <td style="background-color:white;">       
        <input  type="text" id="FROMTIME"  placeholder="HH:MM" name="FROMTIME" value="@Model.FROMTIME" asp-for="FROMTIME" required form="editForm" />
      </td>
    </tr>
    <tr>
      <th>To Time: </th>
      <td style="background-color:white;">
        <input type="text" id="TOTIME"  placeholder="HH:MM" name="TOTIME" value="@Model.TOTIME" asp-for="TOTIME" required form="editForm" />
      </td>
    </tr>
  </table>
</form>

The Ajax call for the backend using serialisation for the data are the following lines of code. The printout at the console is empty because the Time input types are not picked up by jQuery:

使用序列化数据进行后端Ajax调用的代码如下。控制台中的打印输出为空,因为jQuery没有捕获到时间输入类型

$("#editForm").on("submit", function(event) {

    console.log($("editForm").serializeObject());
    $.ajax({
        url: "/Items/SaveItem/",
        type: "POST",
        data: $("#editForm").serializeObject(),
        success: function(data) {
            
            if (data.message == "OK") {
                success("Item has been successfully saved!");
                }
        },
        
    });

});

问题解决:

You might try my codes. You have asp-for taghelper so that I create an asp.net-core MVC application for test. Here's my controller and model.

您可能想试试我的代码。由于您使用了asp-for标签帮助器,所以我将创建一个ASP.NET Core MVC应用程序来进行测试。以下是我的控制器和模型。

public IActionResult SubmitDate() {
    var model = new MyTime { FromTime=DateTime.Now, ToTime=DateTime.Now.AddHours(3)};
    return View(model); 
}

[HttpPost]
public string PostTime(MyTime myTime) { 
    return "success";
}

public class MyTime {
    public DateTime FromTime { get; set; }
    public DateTime ToTime { get; set; }
}

And here's my view.        以下是我的视图

@model WebAppMvc.Controllers.MyTime;

<form id="editForm" asp-action="PostTime" asp-controller="Form">
    <table>
        <tr>
            <th>From Time: </th>
            <td style="background-color:white;">
                <input type="time" placeholder="HH:MM" name="FROMTIME" value="@Model.FromTime" asp-for="FromTime" required form="editForm" />
            </td>
        </tr>
        <tr>
            <th>To Time: </th>
            <td style="background-color:white;">
                <input type="time" placeholder="HH:MM" name="TOTIME" value="@Model.ToTime" asp-for="ToTime" required form="editForm" />
            </td>
        </tr>
    </table>
    <button type="submit">
        Submit
    </button>
</form>

@section Scripts{
    <script>
        $("#editForm").on("submit", function (e) {
            console.log($("#editForm").serialize());
            alert(1);
            e.preventDefault();
            $.ajax({
                url: "/Form/PostTime/",
                type: "POST",
                data: $("#editForm").serialize(),
                success: function (data) {
                    alert(data);
                },
            });
        });
    </script>
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值