开发者

MVC Ajax forms not submitting on page load with jQuery

I am having a problem getting a form to post on load. The code to do the loading is simply:

$(document).ready(function()
{
    $('#resultsFor开发者_Go百科m').triggerHandler('submit');
}

and for the markup, I am simply using an MVC Ajax form:

    <% using (Ajax.BeginForm("resultsForm", new AjaxOptions { UpdateTargetId = "resultsDiv", OnSuccess = "onSuccess" })) { %> 
//html in here  
    <% } %>

In FF and Chrome, the page loads, and then a couple seconds later the div for the results loads as well. This is what I expect to happen. However, in IE nothing happens.

When I try to do

$(document).ready(function()
{
    $('#resultsForm').trigger('submit');
}

The page loads in IE, and then redirects me to a page that is just the results for the form.

Any ideas on how to get around this issue?


After some looking around, I came across a solution for my problem. The solution can be found here, and using my example it would look like:

$(document).ready(function()
{
    var form = $('#resultsForm');
    form.submit(function(event) { eval($(this).attr("onsubmit")); return false; });
    form.submit();
}

This worked for me in all browsers.


I'm not really familiar with MVC Ajax, but normally you would put the submit code for the form into a function and call that instead. ie.

function doThisOnSubmit() {
   //Submit code here
}
$(document).ready(function() {
    doThisOnSubmit();
});

And then your HTML code:

<form onsubmit="doThisOnSubmit();">
<!-- Stuff -->
</form>

Hopefully this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜