阿贾克斯提交使人充实岗位后的第一次提交 [英] Ajax submit makes a full post after first submit

查看:157
本文介绍了阿贾克斯提交使人充实岗位后的第一次提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个提交作品如预期,但下一次它完全贴后背。我使用jQuery.form插件,特别是 .ajaxSubmit(选项)调用提交表单。也有包装div的一对夫妇的水平,但我调试button.click绑定,我不明白,为什么第二次,即使采用相同的变量,它做了全文后,我的部分观点是在一个全新的返回页。

The first submit works as expected, but the next time it's fully posted back. I'm using jQuery.form plugin, specifically the .ajaxSubmit(options) call to submit the form. There are a couple levels of wrapper div's, but I debug the button.click binds and I can't figure out why the second time, even with the same variables, it does a full post and my partial view is returned on a brand new page.

这里, buttonid screenid 上两个职位相同。我指出这一点,因为他们计算为每一个命名约定如<名称>外(包装),<名称> (更新目标),而<名称>表(表单提交)

here, buttonid and screenid are the same on both posts. I indicate this because they're calculated as per a naming convention like <name>Outer (wrapper), <name> (update target), and <name>Form (form to be submitted)

$('.formButton').click(function () {
    var buttonid = $(this).attr('id');
    var screenid = $('#' + buttonid).closest('div:not(.CSRForm)').attr('id').replace('Outer', '');
    //appends a hidden element so I know which button was pressed when posted
    $('#' + screenid + 'Form').append('<input type="hidden" name="submitButton" value="' + buttonid.replace(screenid, '') + '" />');
    $('#' + screenid + 'Form').submit();
}); 

介意 .formButton 元素的不可以的形式,这就是为什么我绑定submit事件本身里面。我的局部视图只返回的形式,而不是按钮(它们是动态的)

mind the .formButton elements are not inside the form, which is why I had to bind the submit event as such. My partial view only returns the form and not the buttons (they are dynamic)

这里是.ajaxSubmit:

here is the .ajaxSubmit:

$('.CSRForm').submit(function () {
    var needsValidation = "yes";
    if (needsValidation) {
        $(this).ajaxSubmit({
            target: '#AccountSetup',
            replaceTarget: false,
            success: StepCallWithForm //to check whether the response is valid for redirecting
        });
        return false;
    }
}); 

和我的MVC的行动,如果是相关的:

and my MVC action if it's relevant:

    public ActionResult AccountSetup(AccountSetupViewModel vm, string submitButton)
    {
        if (!ModelState.IsValid)
        {
            return PartialView(vm);
        }
        else
        {
            return Content(submitButton + ",AccountSetup");
        }
    }

编辑: 立即里面插入此行后,我的 $(函数(){

After inserting this line immediately inside my $(function() {:

$('#AccountSetup').ajaxForm();

我是能够阻止完全回发。在Firebug的,现在我明白了AJAX回发到 / CSR /家庭/企业社会责任?这就是所有这些东西是开始。这就像形式失去了它的action属性。是我的事件在某种程度上crossfiring?

I was able to stop the full postback. In Firebug, now I see ajax postback to /CSR/Home/CSR? which is where all this stuff is to begin with. It's like the form lost its action property. Are my events somehow crossfiring?

推荐答案

而不是绑定到click事件,你可以使用jQuery的现场()事件绑定,它生存的AJAX加载。您可以通过更换做到这一点:

Instead of binding to the click event, you could use jQuery's live() event binding, which survives AJAX loading. You can do this by replacing:

$('.formButton').click(function () {

通过

$('.formButton').live("click", function () {

这篇关于阿贾克斯提交使人充实岗位后的第一次提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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