【问题标题】:IE7 and IE6 do not pass params in a form_tag in RailsIE7 和 IE6 不在 Rails 的 form_tag 中传递参数
【发布时间】:2011-02-08 21:15:03
【问题描述】:

我遇到过的最奇怪的错误。开个玩笑,但它肯定在顶部。

如果我像这样创建了一个动态表单..

= form_tag main_index_path, :method => :post do
  .payment
    = text_field_tag "payments[0][:date_paid]"
    = text_field_tag "payments[0][:amount_paid]"
    %br/
  = link_to 'Add Another Payment', '#', :class => "add_another"

  .actions
    %br/
    = submit_tag 'calculate'

然后点击'Add Another Payment'

一个新的 .payment div 将如下所示:

.payment
  = text_field_tag "payments[1][:date_paid]"
  = text_field_tag "payments[1][:amount_paid]"

奇怪的是,它仅在 IE6 和 IE7 中,它只会通过 first 和 last 付款。

如何/为什么/我可以做些什么来解决这个问题?

作为参考,这里是 'add_another' 方法:

$(".add_another").click(function(){
  if ($(".payment:last").find("input").val() != "") {
    var $newdiv = $(".payment:last").clone(true);
    $newdiv.find('input').each(function() {
        var $this = $(this);
        $this.attr('id', $this.attr('id').replace(/_(\d+)_/, function($0, $1) {
            return '_' + (+$1 + 1) + '_';
        }));
        $this.attr('name', $this.attr('name').replace(/\[(\d+)\]/, function($0, $1) {
            return '[' + (+$1 + 1) + ']';
        }));
        $this.val('');
    });
    $newdiv.find('textarea').each(function(){
      var $this = $(this);
      $this.attr('id', $this.attr('id').replace(/_(\d+)_/, function($0, $1) {
          return '_' + (+$1 + 1) + '_';
      }));
      $this.attr('name', $this.attr('name').replace(/\[(\d+)\]/, function($0, $1) {
          return '[' + (+$1 + 1) + ']';
      }));
      $this.css("color","#cccccc");
    });
    $newdiv.insertAfter('.payment:last').hide().slideDown();
  };
  return false;
});

【问题讨论】:

  • 我建议您只需使用当前时间(以毫秒为单位)作为新付款的 id,您会确保它是唯一的。
  • 您认为这就是它无法通过参数的原因吗?在检查参数时,只有第一个和最后一个通过。
  • 我不认为这是相关的,我应该更清楚一点,只是为了节省一些代码。

标签: jquery html ruby-on-rails internet-explorer params


【解决方案1】:

奇怪的是你收到了第一笔和最后一笔付款。这是一个很长的机会,但作为 IE ..

有什么方法可以检查您的 insertAfter 是否正常工作,因为您的 CSS 选择器 .selector:last 实际上选择了组中的 new 最后一个元素?

我还没有检查过这个但是如果你替换会发生什么:

$newdiv.insertAfter('.payment:last').hide().slideDown();

用这个:

$(".payment").last().after($newdiv).hide().slideDown();

【讨论】:

  • 啊,好点子。我还在这里找到了更多信息,但它就在你的答案旁边。 stackoverflow.com/questions/4939433/… 。我试过你的答案,它返回了这个:An error as occurred in the script on this page. Line 34 ( that's the line ) Object doesn't support this property or method
  • 啊,应该是这个$(".payment:last").after($newdiv).hide().slideDown();,但不,它没有任何作用。
  • 道歉之旅,很高兴你找到了答案;)
【解决方案2】:

IE7 要求您使用 OuterHTML 进行克隆

      var $this = $(this);
      if (!$.browser.msie || parseInt($.browser.version) > 7) {
        $this.attr('id', $this.attr('id').replace(/_(\d+)_/, function($0, $1) {
            return '_' + (+$1 + 1) + '_';
        }));
        $this.attr('name', $this.attr('name').replace(/\[(\d+)\]/, function($0, $1) {
            return '[' + (+$1 + 1) + ']';
        }));
      } else {
        foob_name = $this.attr('name').replace(/\[(\d+)\]/, function($0, $1) {
            return '[' + (+$1 + 1) + ']';
        });
        foob_id = $this.attr('id', $this.attr('id').replace(/_(\d+)_/, function($0, $1) {
            return '_' + (+$1 + 1) + '_';
        }));

        $this.attr("outerHTML", "<input type='test' name=" + foob_name + " id=" + foob_id + " />");

      };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多