jQuery表单提交.停止页面刷新仅在最新的浏览器中有效 [英] jQuery form submission. Stopping page refresh only works in newest browsers

查看:112
本文介绍了jQuery表单提交.停止页面刷新仅在最新的浏览器中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery 1.4来做到这一点.

Using jQuery 1.4 to do this.

向body标签添加一个表单,表单本身没有什么特别之处:

Adding a form to the body tag, nothing special about the form itself:

$(function() {
$('body').prepend('<form name="frmSample" id="frmSample"><input class="btn" type="submit" value="SIGN UP" /></form>');
});

我正在使用以下方式提交表单:

I'm using the following to submit the form:

$('#frmSample').live('submit', function() {
  var formdata = $(this).serialize();

   $.ajax({
       type: 'POST',
       url: 'http://www.example.com/Values.jsp',
       data: formdata,
       success:function() {
       submit_success();
       }
       });
  return false
  });

在最新的浏览器(Firefox 3.5,Safari 4)上,此功能正常运行.一旦我下降到FF 3.0及以下版本或IE 7及以下版本,此操作就会停止.

On the newest browsers (Firefox 3.5, Safari 4) this works fine. Once I go down to FF 3.0 and below or IE 7 and below this stops.

我现在有点卡住了.我一直在寻找很多东西,除了返回false或event.preventDefault()之外,我只能找到提及其他内容的任何东西.

I'm a bit stuck right now. I've been searching for a good bit and have only been able to find anything mentioning something other than return false or event.preventDefault().

所以想看看我是否在这里错过了一些小东西,或者是朝着完全错误的方向前进.

So looking to see if I am missing something small here, or am going in the totally wrong direction.

推荐答案

我会比stopPopagation更进一步:

I would go a step further than stopPopagation:

$('#frmSample').live('submit', function(e) {  
    stopEvent(e);  
    ....etc...etc

以及stopEvent的代码:

And the code for stopEvent:

function stopEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    if ($.browser.msie) {
        event.originalEvent.keyCode = 0;
        event.originalEvent.cancelBubble = true;
        event.originalEvent.returnValue = false;
    }
}

从此处: http://www.openjs.com/articles/prevent_default_action/

returnValue和preventDefault()方法

这可以满足我们的需要-但是我们不能依靠return false方法来阻止默认操作.提供此代码将退出该功能-因此,如果在该功能中还有很多事情要做,则无法使用此代码.在我遇到的大多数情况下,返回false就足够了,但是在某些情况下,我们需要更激烈的事情.

That does what we need - but we cannot rely on the return false method for preventing the default action. Giving this code will exit the function - so if there is more to do in the function, this code cannot be used. In most of the situations I encounter, return false is enough, but in some areas, we need something more drastic.

在Google中键入以下内容以获取更多信息:

Type the following into google for more info:

阻止默认stopPropagation cancelBubble

preventDefault stopPropagation cancelBubble

这篇关于jQuery表单提交.停止页面刷新仅在最新的浏览器中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆