另一种方法是让chrome、IE 8和firefox提交相同的表单

wierd way to get chrome, IE 8 and firefox to submit the same form

本文关键字:提交 firefox 表单 方法 chrome IE 另一种      更新时间:2023-09-26

这是我找到的让所有三个浏览器都能毫无问题地提交表单的唯一方法。有明显的原因吗?一个更优雅的解决方案?我使用的是jQuery 1.9。Chrome是一个奇怪的人,因为其他地方的代码足以通过IE和Firefox提交。

function submitFormByPost(actionName){ 
     $("#eventAction").val(actionName);
     var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
     if(is_chrome){
          document.getElementById('myForm').method='POST';
          document.getElementById('myForm').submit();
     }
     else{
          document.forms[0].method='POST';
          document.forms[0].submit(); 
     } 
}
jQuery应该已经提供了一种跨浏览器提交表单的方式
var $form = $("#myForm");
$form.attr('method', 'post');
$form.submit();

在Chrome中的工作方式也会在其他浏览器中工作,所以只需使用:

function submitFormByPost(actionName){ 
  $("#eventAction").val(actionName);
  var frm = document.getElementById('myForm');
  frm.method = 'POST';
  frm.submit();
}

或者一直使用jQuery:

function submitFormByPost(actionName){ 
  $("#eventAction").val(actionName);
  $('#myForm').attr('method', 'POST').submit();
}

这适用于一些较旧的Chrome版本。通常,document.forms[0]在Chrome浏览器上和在其他浏览器上一样工作。最简单的检查方法,打开Chrome控制台并编写console.log(document.forms[0]);-工作正常。