英文:
What makes preventing the default form submission behavior fail in this use case of jQuery AJAX?
问题
-
我已经制作了一个使用Laravel 8的**博客应用程序**。
-
我目前正在尝试通过AJAX提交评论和回复评论,而不是以“传统”的方式。
-
评论表单:
<div id="commentSuccess-{{ $comment->id ?? '' }}" class="alert-box-ajax alert-box alert-box--success">
您的评论正在等待审核
</div>
<div id="commentFail-{{ $comment->id ?? '' }}" class="alert-box-ajax alert-box alert-box--error">
添加评论失败!
</div>
<form class="commentForm" method="post" action="{{ route('comment.submit') }}" autocomplete="off">
@csrf
<fieldset>
<input type="hidden" name="article_id" value="{{ isset($article->id) ? $article->id : $article_id }}">
<input type="hidden" name="parent_id" value="{{ $comment->id ?? '' }}">
<div class="message form-field">
<textarea name="msg" id="message" class="h-full-width" placeholder="您的留言" required></textarea>
@error('msg')
<p class="help-block text-danger">{{ $message }}</p>
@enderror
</div>
<br>
<input name="submit" id="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="添加评论" type="submit">
</fieldset>
</form>
-
为了验证,我使用了**jQuery验证插件**(版本1.19.0)由Jörn Zaefferer制作。
-
(jQuery)AJAX部分:
// 验证评论表单
$(".commentForm").each(function() {
var form = $(this);
form.validate({
errorElement: 'p',
errorClass: "help-block text-danger",
submitHandler: function(event) {
event.preventDefault();
var $fields = form.find('textarea'),
url = form.attr('action'),
data = form.serialize();
$.ajax({
dataType: "json",
type: "post",
url: url,
data: data,
cache: false,
success: function(response) {
$('#commentSuccess-' + data.article_id).slideDown(250).delay(2500).slideUp(250);
$fields.val('');
},
error: function() {
$('#commentFail-' + data.article_id).slideDown(250).delay(2500).slideUp(250);
}
});
}
});
});
- 问题:
由于某种原因,我无法找出,应该阻止“传统”表单提交的部分,event.preventDefault()
不起作用。
-
问题:
-
我做错了什么?
-
修复这个问题最可靠的方法是什么?
英文:
I have made a blogging application in Laravel 8.
I am currently working on submitting comments and comment replies via AJAX instead of doing it "classically".
The comment form:
<div id="commentSuccess-{{ $comment->id ?? '' }}" class="alert-box-ajax alert-box alert-box--success">
Your comment is pending
</div>
<div id="commentFail-{{ $comment->id ?? '' }}" class="alert-box-ajax alert-box alert-box--error">
Failed to add comment!
</div>
<form class="commentForm" method="post" action="{{ route('comment.submit') }}" autocomplete="off">
@csrf
<fieldset>
<input type="hidden" name="article_id" value="{{ isset($article->id) ? $article->id : $article_id }}">
<input type="hidden" name="parent_id" value="{{ $comment->id ?? '' }}">
<div class="message form-field">
<textarea name="msg" id="message" class="h-full-width" placeholder="Your Message" required></textarea>
@error('msg')
<p class="help-block text-danger">{{ $message }}</p>
@enderror
</div>
<br>
<input name="submit" id="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
</fieldset>
</form>
For validation, I use the jQuery Validation Plugin (v1.19.0) by Jörn Zaefferer
The (jQuery) AJAX part:
// Validate comment form
$(".commentForm").each(function() {
var form = $(this);
form.validate({
errorElement: 'p',
errorClass: "help-block text-danger",
submitHandler: function(event) {
event.preventDefault();
var $fields = form.find('textarea'),
url = form.attr('action'),
data = form.serialize();
$.ajax({
dataType: "json",
type: "post",
url: url,
data: data,
cache: false,
success: function(response) {
$('#commentSuccess-${data.article_id}`').slideDown(250).delay(2500).slideUp(250);
$fields.val('');
},
error: function() {
$(`#commentFail-${data.article_id}`).slideDown(250).delay(2500).slideUp(250);
}
});
}
});
});
The problem
For a reason I have been unable to find out, the part that should prevent "classic" form submission, event.preventDefault()
does not work.
Questions:
- What am I doing wrong?
- What is the most reliable way to fix this problem?
答案1
得分: 1
在进行了一些研究后,我发现事件实际上是提交处理程序(submitHandler)中接收的第二个参数。第一个参数是表单本身。
由于某些原因,文档与实际情况不同步。
您可以尝试使用以下代码来阻止表单的默认提交行为。
submitHandler: function(form, event) {
event.preventDefault();
}
希望这对您有所帮助。
英文:
After doing some research i have found that event is actually second parameter received in the submitHandler. The first one is form itself.
For some reasons the documentation is not in a sync.
You can try this to prevent the form default form submission.
submitHandler: function(form, event) {
event.preventDefault();
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Validate comment form
$(".commentForm").each(function(){
var form = $(this);
form.validate({
errorElement: 'p',
errorClass: "help-block text-danger",
submitHandler: function(_form, event) {
event.preventDefault();
var $fields = form.find('textarea'),
url = form.attr('action'),
data = form.serialize();
$fields.val('');
console.log("Submitting to "+url+" with data: "+data);
}
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="commentForm" method="post" action="/commentSubmit" autocomplete="off">
<fieldset>
<input type="hidden" name="article_id" value="33">
<input type="hidden" name="parent_id" value="1">
<div class="message form-field">
<textarea name="msg" class="h-full-width" placeholder="Your Message" required></textarea>
</div>
<br>
<input name="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
</fieldset>
</form>
<form class="commentForm" method="post" action="/commentSubmit1" autocomplete="off">
<fieldset>
<div class="message form-field">
<textarea name="msg" class="h-full-width" placeholder="Your Message" required></textarea>
</div>
<br>
<input name="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
</fieldset>
</form>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<!-- end snippet -->
答案2
得分: 0
不太熟悉jQuery验证插件,但我希望将验证代码放在文档就绪函数内会有一些改变。
$(document).ready(function() {
$(".commentForm").each(function(){
var form = $(this);
form.validate({
// ...
});
});
});
或者
$(function() {
$(".commentForm").each(function(){
var form = $(this);
form.validate({
// ...
});
});
});
英文:
Not so familiar with jQuery validation plugin but I hope putting the validation code inside a Document Ready function would make some change.
$( document ).ready(function() {
$(".commentForm").each(function(){
var form = $(this);
form.validate({
....
})
});
OR
$(function() {
$(".commentForm").each(function(){
var form = $(this);
form.validate({
....
})
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论