javascript - 单击调用javascript函数以隐藏输入的按钮时提交表单
问题描述
下面的代码示例代表了我的问题,简而言之,当我单击按钮并执行 togglehider() 时,div 中的元素确实崩溃了,但随后表单被提交,就像我单击了提交输入一样!有人可以告诉我这是为什么,以及补救措施吗?谢谢你。
@model TUTCSkeleton.ViewModels.CycleEventViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CycleEvent</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
@if (Model.CanManage)
{
<button onclick="togglehider()">Audit Data Visibility</button>
<div id="togglehide">
<div class="form-group">
@Html.LabelFor(model => model.Modified, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.Modified, new {htmlAttributes = new {@readonly = "readonly", @class = "form-control"}})
@Html.ValidationMessageFor(model => model.Modified, "", new {@class = "text-danger"})
</div>
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
<script type="text/javascript">
function togglehider() {
var x = document.getElementById("togglehide");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
}
解决方案
推荐阅读
- xamarin.forms - 应用程序在 iOS 中为 Xamarin.Forms 运行时未收到推送通知
- c# - TimeZoneInfo DST 与 Windows 设置
- python - 使用 Heroku/Postgresql 部署的 Django 应用程序 - 一些 Python/JS 公式仅在生产环境中不起作用,值会跳回以前的值
- xamarin - 用于按钮背景颜色转换的 CustomRenderer
- javascript - 此集合实例上不存在属性 [spesialisasi]
- maven - 我在 HSQLDB 中看不到我创建的表,可能是什么问题?
- python - 如何使用 python 正则表达式从服务器日志文件中提取 IP 地址?
- mysql - 从另一个选择插入另一个表不起作用
- ios - Segue 与 pass 数据工作两次
- javascript - 为什么这不保存