[英]How do I make sure my javascript animation runs before submit button is done?
Attached code below.下面附上代码。 I have a submit button that uses a form method POST, and then it's supposed to run the javascript animation, though it kicks me to the next page before it gets the chance to run it.
我有一个使用表单方法 POST 的提交按钮,然后它应该运行 javascript 动画,尽管它在有机会运行它之前将我踢到下一页。
Is there a way I can add a delay to the submission such that the animation is completed first?有没有办法可以在提交中添加延迟,以便首先完成动画? In this code, it doesn't even get a chance to run the javascript.
在这段代码中,它甚至没有机会运行 javascript。
<div class="container">
<div class="pt-5 text-white">
<link rel="stylesheet" href="css/postQuestion.css">
<br>
<br>
<br>
<br>
<h1>Post a question</h1>
<br>
<form method="POST" action="{{ route("post_post") }}">
@csrf
{{-- Title box --}}
<div class="none" style="margin:0 auto;">
<input class="search" name="title" type="text" id="search" placeholder="Title" required/>
</div>
<br>
<div class="none">
{{-- Question box --}}
<textarea required name="content" class="search2" type="text" id="search" placeholder='Explain your question here.
Protip: add your code as <code> YOUR CODE HERE </code> to format correctly.'></textarea>
</div>
<br/>
<div class="dropdownTag">
<select name="label_id" class="dropdownTag-select" required>
<option value="-1">Select Tag</option>
@foreach(\Illuminate\Support\Facades\DB::table("tags")->orderBy('name', 'asc')->get() as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
@endforeach
</select>
</div>
<br/>
<div class="container">
<button id="button" type="submit"></button>
<form submit="return function();">
</div>
</form>
</div>
<script>
$(function () {
$("#button").click(function () {
$("#button").addClass("onclic", 250, validate);
});
function validate() {
setTimeout(function () {
$("#button").removeClass("onclic");
$("#button").addClass("validate", 450, callback);
}, 2250);
}
function callback() {
setTimeout(function () {
$("#button").removeClass("validate");
}, 1250);
}
});
</script>
</div>
If you need to use the "submit", then you can simply hide the submit button itself, and hang the listener on the button that will call its animations, and then, when you need it, submit.如果您需要使用“提交”,那么您可以简单地隐藏提交按钮本身,并将侦听器挂在将调用其动画的按钮上,然后在需要时提交。
...
<div class="container">
<button id="button" type="button"></button>
<input id="submit" type="submit" hidden />
<form submit="return function();">
</div>
...
...
$("#button").click(function (event) {
event.preventDefault();
$("#button").addClass("onclic", 250, validate);
...
$("#submit").click();
});
...
It is also possible without creating a hidden input through the form submission.也可以不通过表单提交创建隐藏输入。 I think this option is better.
我认为这个选项更好。
...
<form id="someform" method="POST" action="{{ route("post_post") }}">
...
<div class="container">
<button id="button" type="button"></button>
<form submit="return function();">
</div>
...
...
$("#button").click(function (event) {
event.preventDefault();
$("#button").addClass("onclic", 250, validate);
...
$("#someform").submit();
});
...
Using event.preventDefault() you can halt the normal behavior of the form.使用event.preventDefault()您可以停止表单的正常行为。 After that you can implement whatever you like before manually submitting the form from JS.
之后,您可以在从 JS 手动提交表单之前实现您喜欢的任何内容。
If what you need is to wait some time before submitting, you can then use setTimeout() .如果您需要在提交前等待一段时间,则可以使用setTimeout() 。
Then manually submit the form using submit() function.然后使用submit() 函数手动提交表单。
Vanilla JS香草JS
<script>
document.addEventListener('DOMContentLoaded', function () {
document.forms['test_form'].addEventListener('submit', function (e) {
e.preventDefault();
form = this;
// Make a new timeout set to go off in 1000ms (1 second)
setTimeout(function () {
// submit form after timeout
form.submit();
}, 1000);
});
});
</script>
<form name="test_form" method="POST" action="{{ route('post_post') }}">
<label for="test">Test field</label>
<input type="input" name="test"/>
<input type="submit"/>
</form>
document.addEventListener('DOMContentLoaded', function() { document.forms['test_form'].addEventListener('submit', function(e) { e.preventDefault(); form = this; // Make a new timeout set to go off in 1000ms (1 second) setTimeout(function() { // submit form after timeout form.submit(); }, 1000); }); });
<form name="test_form" method="POST" action="{{ route('post_post') }}"> <label for="test">Test field</label> <input type="input" name="test" /> <input type="submit" /> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.