我今天遇到了一个问题。我已经解决了。我试探性地解决了它,但是我想要科学解释(我们不应该是程序员,科学家也是:) :)
这是我的MVC项目的初始View代码:
//shortened for brevity
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.UserName, new { onkeyup = "InputToLower(this);" })
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserPassword)
</div>
<div class="editor-field">
<input type="password" name="password1" id="password1" />
</div>
<div class="editor-label">
<label for="male">Lütfen şifrenizi tekrar giriniz: </label>
</div>
<div class="editor-field">
<input type="password" name="password2" id="password2" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserEmail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
<p>
<input type="submit" id="registerButton" value="Kayıt Ol" />
</p>
</fieldset>
}
<script type="text/javascript">
$(function () {
$("#registerButton").click(function (e) {
// e.preventDefault();
var errorSummary = $('.validation-summary-errors');
if (errorSummary.length == 0) {
$('#listError').remove();
$('<div class="validation-summary-errors"></div>').insertAfter($('.validation-summary-valid'));
$(".validation-summary-errors").append("<ul id='listError'><li>0 karakter giremezsiniz. OSI-122 </li></ul>");
}
else if (errorSummary.length == 1) {
$('#listError').remove();
$(".validation-summary-errors").append("<ul id='listError'><li>You cannot enter more than 20 characters.</li></ul>");
}
//return false;
});
});
</script>
为简洁起见,缩短了代码。当我禁用 e.preventDefault()和返回false 时,已完成发布。如何以及为什么 e.preventDefault()和返回false 会阻止发布?你推荐任何提到这类问题的书吗?提前致谢。
答案 0 :(得分:0)
registerButton是提交按钮,提交的行为是将表单的数据发布到表单标记中提到的操作。如果您没有在表单标记中提及任何操作属性,那么它将触发相同获取操作的后期操作。
event.preventDefault()并返回false会停止您的回发或其他事件。