1
今天我遇到了問題。我解決了它。我解決了它啓發,但是我想科學的解釋(我們不應該是一個程序員,一個科學的人也:))爲什麼「e.preventDefault()」和「返回false」會禁用帖子?
這是我最初的查看我的MVC項目代碼:
//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防止發佈?你推薦任何提到這種問題的書嗎?提前致謝。
https://api.jquery.com/event.preventdefault/和http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false它們不是問題,它的行爲有阻止張貼。 – Satpal
謝謝你的回答先生。看來在互聯網上搜索是顯而易見的方式。用於JS的 – tahasozgen
訪問https://developer.mozilla.org/en-US/docs/Web/JavaScript – Satpal