简体   繁体   English

如何确保我的 javascript 动画在提交按钮完成之前运行?

[英]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.

相关问题 在将数据推送到数据库之前,如何确保 if 函数运行? - How do I make sure my if funtion runs before my data is pushed to a database? 使用模糊事件,我如何确保模糊事件在提交按钮之前发生? - Using blur event, how do I make sure the blur event goes before the submit button? 如何确保在Mocha中的done()之前执行对数据库的循环保存? - How do I make sure a looping save to the database executes before done() in Mocha? 如何确保在导航到另一个页面之前完成减速器响应操作? - How do I make sure that reducer response action is done before navigating to another page? 如何确保 getData() 在 MovieCard function 之前运行? - How do make sure getData() runs before MovieCard function? 如何确保在前一个块完成后运行一个 JavaScript 块? - How do I make sure a block of JavaScript runs after previous block has finished? 如何创建一个运行我的 javascript 函数的按钮? - How do I create a button that runs my javascript function? 如何确保我的Javascript动画停留在其他固定元素之下? - How can I make sure my Javascript animation stay under other stationary elements? 如何让我的提交按钮在点击时消失? - How do I make my submit button disappear onclick? Javascript-确保在执行下一个脚本之前执行一个脚本 - Javascript - make sure one script is done before next is executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM