【发布时间】:2019-07-23 19:47:20
【问题描述】:
浏览一些旧代码,似乎 e.preventdefault() 在单击按钮时表现不正确。我不知道我以前怎么没有注意到这一点,但由于某种原因,它只是 Simpy 没有做它的工作。即使执行了 If 语句,页面仍然会刷新。我错过了什么,我是瞎了吗?谁能告诉这里发生了什么?
另外,我想补充一点,它肯定会进入 IF 语句,因为需要取消隐藏的 DIV 确实会显示,但是页面无论如何都会刷新并且它不应该提交。
下面是我的代码:
谢谢。
表格:
<form class="form-group" method="get" action="#">
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Terminal</th>
<th>Command</th>
<th>Command Value</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.DropDownListFor(o => o.TerminalsDDL, Model.TerminalsDDL, new { id = "ddlTerminalID", @class = "form-control align-middle" })</td>
<td onchange="UpdateHelpDescript();">@Html.DropDownListFor(o => o.TerminalCommandLookupsDDL, Model.TerminalCommandLookupsDDL, new { id = "ddlCommandValue", @class = "form-control align-middle" })</td>
<td>@Html.TextBoxFor(o => o.UserEnteredTerminalCommands, new { Class = "form-control align-middle", Id = "cmdValueValue" })</td>
<td> <input id="btnSaveTerminalCommand" type="button" value="Insert" class="btn btn-primary align-middle" /> </td>
</tbody>
</table>
</div>
</div>
</form>
JS:
$(function () {
$("#btnSaveTerminalCommand").click((e) => {
if ($("#ddlCommandValue").val() == " " || $("#ddlCommandValue").val() == null || $("#ddlCommandValue").val() == 0 || $("#cmdValueValue").val() == " " || $("#cmdValueValue").val() == null) {
//window.alert("Please select a valid command.");
$("#alertPopUp").removeClass("hidden");
e.preventDefault();
e.stopImmediatePropagation();
};
var data = {
TerminalID: $("#ddlTerminalID").val(),
CommandID: $("#ddlCommandValue").val(),
CommandValue: $("#cmdValueValue").val()
};
$.ajax({
url: '@Url.Action("AddTerminalCommand", "TerminalCommand")',
type: "POST",
data: data,
success: function (response) {
console.log("Success");
window.location.href = response.Url;
}
});
});
});
【问题讨论】:
标签: javascript c# jquery asp.net-mvc razor