duanjiu1894 2013-04-08 01:39
浏览 32
已采纳

jquery .submit()在setTimeout中不会向php传递数据

I am trying to add an effect to the login form and when the effects is done ( like 1 sec) it then goes to php.

I tried to make a setTimeout() function inside the .submit with e.preventDefault() so I can delay it for a sec but the problem is it didnt take the data but instead it goes to the php in a blank webpage that sopposed to be checking the data that was inputted.

And when the e.preventDefault() is been taken away the php works but it didnt give me a second to perform the animation first then go to php file to check all the data

here is my code

<script> $("#effectsExplode").submit(function (e) {
        var form = this;
        e.preventDefault();
        $("#effectsExplode").toggle("explode");
        setTimeout(function () {
            form.submit();
        },1000);
    });
</script>




<form id="effectsExplode" class="form-1" method="post" action="checklogin.php">
    <p class="field">
        <input type="text" name="login" placeholder="Username or email">
        <i class="icon-user icon-large"></i>
    </p>
    <p class="field">
        <input type="password" name="password" placeholder="Password">
        <i class="icon-lock icon-large"></i>
    </p>
    <p class="submit">
        <button id="buttonexplode" type="submit" name="loginsubmit">
            <i class="icon-arrow-right icon-large"></i>
        </button>
    </p>
</form>
  • 写回答

1条回答 默认 最新

  • doutang7383 2013-04-08 01:56
    关注

    You've created an infinite loop. If for example I had a button and this javascript:

    $('button').click(function() {
        var b = this;
        $('#status').append('Clicked<br/>');
        setTimeout(function() {
            b.click();
        }, 1000);
    });
    

    http://jsfiddle.net/b9chris/yaNdc/

    Once I click that button, I'll get another "Clicked" message appended to that status tag every second... forever. That's because jquery fires all its event handlers for the events you manually trigger; in this case you attached to 'submit' then fired 'submit' and looped right back into your own code, which keeps preventing the form from ever actually submitting.

    Most likely on your local test machine this form submission happens almost instantly, but once you put it out on the webserver the live version will take longer, and your animation will have time to play. Simplest solution is to just get rid of the ev.preventDefault() and let it submit and play while the submission takes its time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值