如何在javascript提交表单后在输入框中显示日期字段?



提交表单后,我需要在同一页面的输入框中显示用户选择的日期。我是指这个链接,如果我试图在我的代码中实现相同的,我的表单没有提交,我无法看到所需的输出。

下面是我的代码: HTML:

<form method="POST" id="myform">
<div class="de-section de-cart-stat d-flex align-items-center flex-wrap mt-4">
StartDate:<input type="date" id="stdate" name="stname">
EndDate:<input type="date" id="eddate" name="edname">
<button id="submitbtn" type="submit">Submit</button>

JS:

$("#submitbtn").click(function() {
if ($("#stdate").val().length === 0 || $("#eddate").val().length === 0) {
$("#errorModal").modal("show");
return false;
}
else{
$("form").submit();
}
});

在使用javascript单击提交按钮后,在输入框中显示日期。

如果你想要鱼和熊掌兼得,使用AJAX

$("#myform").on("click", function(e) {
e.preventDefault(); // stop submission 
if ($("#stdate").val().length === 0 || $("#eddate").val().length === 0) {
$("#errorModal").modal("show");
return;
}
$.post("someUrl",$(this).serialize(),function(res) {
$("someContainer").html(res)
});  
});

最新更新