2014-12-27 178 views
0

我这种形式:形式通过Ajax提交不工作

<form method="post" id="userForm" name="userForm" class="form-validate" action="/istuff/index.php/user" > 
<input type="hidden" name="option" value="com_virtuemart"/> 
<input type="hidden" name="view" value="user"/> 
<input type="hidden" name="controller" value="user"/> 
<input type="hidden" name="task" value="saveUser"/> 
<input type="hidden" name="layout" value="edit_address"/> 
<input type="hidden" name="address_type" value="ST"/> 
<button class="button" type="submit" 
</form> 

而且我对这种形式的AJAX脚本:

jQuery(document).ready(function($) { 
    $("#userForm").live("submit", function (event) { 
    event.preventDefault(); 
    var form = $(this); 
    $.ajax({ 
     url: form.attr("action"), // Get the action URL to send AJAX to 
     type: "POST", 
     data: form.serialize(), // get all form variables 
     success: function(result){ 
      // ... do your AJAX post result 
     } 
    }); 
    }); 
}); 

形式运作良好没有AJAX,但后来我加入这个脚本它不起作用。此外,我试图去到AJAX调用中使用的链接(带有参数的复制位置(带有提交的参数的完整链接) - 它将我重定向到完全不同的页面,然后我提交没有AJAX的表单。

+0

你缺少的数据类型:“???”和contentType:“application/???”在你的ajax调用中 – agentpx 2014-12-27 14:21:08

回答

0

I希望这将这样的伎俩:解决这个

jQuery(document).ready(function() { 
    $("#userForm").submit(function() { 
    var form = $("$userForm"); 
    $.ajax({ 
     url: form.attr("action"), // Get the action URL to send AJAX to 
     type: "POST", 
     data: form, 
     success: function(result){ 
      // ... do your AJAX post result 
     } 
    }); 
    return false; 
    }); 
}); 
1

一种方法是:

1)在“形式”的声明删除“行动”属性和更改按钮,一个普通按钮(不提交)

2)在jQuery ajax调用中,直接提供网址。

$.ajax({ 
     url: "/istuff/index.php/user", 

它也值得推荐的做法表单提交时禁用按钮,使它回来一次Ajax响应回来,以防止用户再次点击按钮。您还可以提供进一步的视觉指示,表明请求正在处理中。我喜欢为此使用blockUI jquery插件。

0

从jQuery 1.7开始,不推荐使用.live()方法。使用.on()附加事件处理程序。

我想你的代码通过更改下面一行,它的工作,

$("#userForm").on("submit", function (event) {