Chrome _blank表单和JQuery双提交

时间:2013-01-06 13:27:17

标签: jquery cross-browser

我有一张表格:

<form accept-charset="UTF-8" action="/back/pages/22?locale=fr" class="form-horizontal page-form" enctype="multipart/form-data" id="edit_page_22" method="post" target="_blank"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
  <p><a href="#" id="no_window">Submit</a></p>
  <p><a href="#" id="new_window">Submit & _blank</a></p>
</form>

附上一个剧本:

  $(document).ready(function() {
    var page_form;
    page_form = {
      elem: $('form.page-form'),

      rand_action: function() {
        var action;
          action = this.elem.attr('action');
          return this.elem.attr('action', action + '&count=' + Math.random() * 1000);
      },
      submit_blank: function() {
        this.elem.attr('target', '_blank');
        $('<input>').attr({
          type: 'hidden',
          id: 'input_new_window',
          name: 'edit_redirect',
          value: 'true'
        }).appendTo(this.elem);
        return this.elem.submit();
      },
      submit: function() {
        this.elem.attr('target', '_self');
        $('#input_new_window').remove();
        return this.elem.submit();
      }
    };

    $("#new_window").click(function(event) {
      event.preventDefault();
      page_form.submit_blank();
      page_form.rand_action();
      return page_form.submit();
    });
    $("#no_window").click(function(event) {
      event.preventDefault();
      return page_form.submit();
    });
  }); 

它适用于FF,但不适用于Chrome,任何想法为什么?

1 个答案:

答案 0 :(得分:0)

这是基于默认浏览器行为,除非指定要打开新选项卡,否则无法使用chrome执行此操作。这可以使用window.open()方法完成:

SEE DEMO

$(document).ready(function() {
    var form;
    form = $('form.page-form');
    $("#submit_blank").click(function(event) {
      event.preventDefault();
      $('<input>').attr({
        type: 'hidden',
        id: 'input_new_window',
        name: 'edit_redirect',
        value: 'true'
      }).appendTo(form);
      form.submit();
      return window.open(window.location.href.replace(/form/, ''));
    });

    $("#no_window").click(function(event) {
      event.preventDefault();
      form.attr('target', '_self');
      return form.submit();
    });
  }); 

注意:您应该添加一个测试来获取当前正在运行的浏览器,以避免出现任何意外行为,因为每个浏览器在此浏览器上的工作方式完全不同。