我有以下代码,其中包含按钮,click
上应显示hidden
表单。但它没有,为什么?
@model EmployeesTest.Models.EmployeeVM
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<input type="submit" name="name" value="New Entry" id="new"/>
@using (Html.BeginForm()) {
<div id="frm" style="visibility:hidden">
<table>
<thead>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.LabelFor(x=>x.emp.FirstName)</td>
<td>@Html.TextBoxFor(x=>x.emp.FirstName)</td>
<td>@Html.ValidationMessageFor(x => x.emp.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(x=>x.emp.LastName)</td>
<td>@Html.TextBoxFor(x=>x.emp.LastName)</td>
<td>@Html.ValidationMessageFor(x => x.emp.LastName)</td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
</tbody>
</table>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function () {
$('#new').click(function () {
(this).preventDefault();
$('#frm').css('visibility', 'visible');
});
});
</script>
}
答案 0 :(得分:1)
删除(this).preventDefault
并传递event
作为参数,并将其阻止如下:
$(document).ready(function () {
$('#new').click(function (event) {
event.preventDefault();
$('#frm').css('visibility', 'visible');
});
});
由于
button
的类型只是button
,因此没有必要 防止其默认操作。
答案 1 :(得分:1)
$(document).ready(function () {
$('#new').click(function (e) {
e.preventDefault();
$('#frm').css('visibility', '');
});
});