2015-09-29 45 views
0

我发现了很多关于此的帖子,但我无法得到这个为我的生活工作。在提交表格时,我需要代码1)提交表格并2)重定向到案例陈述中的一个位置。我可以在没有案件的情况下提交表单,并且我可以让表单重定向,但是我无法将它们同时做到这一点。请告诉我我正处于正确的轨道上,但只是在错误的地方有代码,我一直盯着这个方式太长,并且遇到了障碍。Jquery提交表单,然后重定向不工作

$("#submitBtn").click(function (event) { 
     event.preventDefault(); 
     var validator = $(this).closest("form").kendoValidator({ 
      messages: { 
       required: function (input) { return getRequiredValidationMessage(input) }, 
       custom: function (input) { return getInvalidValidationMessage(input) }, 
      }, 
      rules: { 
       custom: function (input) { 
        var minlength = $(input).attr('data-minlength'); 
        var required = $(input).attr('required'); 
        if (typeof minlength !== typeof undefined && minlength !== false && ((typeof required !== typeof undefined && required !== false) || $(input).val().length > 0)) { 
         var minlength = $(input).data('minlength'); 
         return $(input).val().length >= minlength; 
        } else { 
         return true; 
        } 
       } 
      } 
     }).data("kendoValidator"); 
     if (validator !== undefined) { 
      if (validator.validate()) { 
       $("aspnetForm").submit(); 

       if ($("aspnetForm").submit()){ 
        switch(document.getElementById('interests').value){ 
         case "stair-lifts": 
         window.location.href="/Catalog/Online-Catalog-Category/15522/Stair-Lifts"; 
         break; 

         case "wheelchair-ramps": 
         window.location.href="/Catalog/Online-Catalog-Category/15521/Ramps"; 
         break; 

         case "roll-in-barrier-free-showers": 
         window.location.href="/Catalog/Online-Catalog-Category/15529/Bathroom-Safety"; 
         break; 

         case "walk-in-tubs": 
         window.location.href="/Catalog/Online-Catalog-Category/15529/Bathroom-Safety"; 
         break; 

         case "patient-lifts-ceiling-lifts": 
         window.location.href="/Catalog/Online-Catalog-Category/15523/Patient-Lift"; 
         break; 

         case "wheelchair-lifts": 
         window.location.href="/Catalog/Online-Catalog-Category/15525/Wheelchair--Scooter-Lifts"; 
         break; 

         default: 
         window.location.href="/"; // if no selection matches then redirected to home page 
         break; 
        }// end of switch 
       } 

      } else { 
       $('.k-invalid:first').focus(); 

       $('.k-invalid').blur(function() { if (this.checkValidity()) { $('.k-invalid:first').focus(); } }); 
      } 
     } else { 
      $(this).closest("form").submit(); 
     } 

    }); 
}); 

回答

0

表单提交和页面重定向发生冲突,因为在基本网页中,只有其中一个可能发生。当表单提交时,它将通过所需的方法将表单数据发送到服务器 - 通常是GETPOST

相反,当重定向switch语句运行时,它会告诉浏览器更改页面url。

这两个事件都会触发浏览器中的页面更改,但只有一个可以真正发生。

最能帮助你的是通过AJAX请求提交表单数据,然后在完成时调用重定向代码。

由于看起来您已经在使用jQuery,请查看post method here

1

您不应该为此使用.submit()。由于您使用的JQuery的,你应该使用AJAX请求,格式是这样的:

$("#submitBtn").click(function (event) { 
    $.ajax({ 
     url : "/your/url", // Whatever URL you're submitting to goes here 
     type : "post", 
     data : yourData, // Whatever data you're sending goes here 
     success : function(data) { 
      // Whatever you want to do on success goes here 
     } 
    }); 
}); 

你让在AJAX查询成功执行该功能的URL部分的开关。

+0

嗯,似乎没有与.NET工作,我想唯一的方法来做到这一点是创建一个隐藏的字段,存储的价值,然后重定向提交。 – jlg

+0

你可以更新你的帖子以显示你目前使用的是什么?我不认为将任何内容存储在隐藏字段中都会起作用,因为当您执行'.submit()'时,会导致整个页面重新加载。任何隐藏的字段最终都会随着页面的其余部分一起被丢弃。 –