用Javascript提交表单

用Javascript提交表单

本文介绍了用Javascript提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些形式是使用Javascript/JQuery提交的,因此不会刷新页面.

Some forms of mine are submitted with Javascript/JQuery, so no page refresh occurs.

但是我注意到我的两台计算机之间的浏览器行为异常.在一台计算机(以及我测试过的所有浏览器)上,我的表单提交按预期执行.我的另一台计算机(Mac)屡见不鲜.我将提交表单,它将进行典型的POST刷新.这样浏览器就会刷新,并且在网址"地址栏中看到我按钮的文本.

But I've noticed a weird behavior with browsers between my 2 computers.On one computer (and all browsers I've tested) my form submission executes as expected.My other computer (Mac) it's hit and miss. I'll submit the form, and it will do a typical POST refresh. So the browser refreshes, and I see my button's text in the Url address bar.

-同样,我的PC浏览器不执行此操作.

-again, my PC's browsers do not do this.

除非我发疯,否则我可以推断的是Mac上的浏览器无法正确执行javascript.但是我更愿意在编码方面遇到错误.

Unless I'm going crazy, all I can deduct is that the browsers on my Mac are not executing the javascript properly. But I'm more prepared to expect an error on my coding side.

我是否正确地确保表单不随Javascript一起提交?

Am I properly ensuring the form is NOT getting submitted with my Javascript?

<form id="norefreshForm">
   <!--more form fields here -->
   <input type="submit" name="button" value="Submit Me" />
</form>

<script language="javascript" type="text/javascript">
$(document).ready(function(){

    $.ajaxSetup({
       global:false,
       cache:false,
       type:"POST",
       url:"<?=$_SERVER['REQUEST_URI'];?>",
       timeout:120000
    });


    $("form#norefreshForm").submit(function(){

        $.ajax({ success:function(response){ alert("Submitted"); } });
        return false;
    });

});
</script>

推荐答案

实际上,某些浏览器应该使用e.preventDefault()而不是return false;,我真的不知道为什么.但是这件事在我身上很多发生.因此,您可以执行以下操作:

actually some browsers you should do e.preventDefault() instead of return false; and I really don't know why. but it happened to me a lot. So, you can do this:

 $("form#norefreshForm").submit(function(){
        e.preventDefault();//should be at the very top
        $.ajax({ success:function(response){ alert("Submitted"); } });

    });

});

这篇关于用Javascript提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:16