繁体   English   中英

克隆和更改onchange参数在IE中不起作用

[英]clone and change onchange arguments doesn't work in IE

我的表单上有一个表,该表具有一行或多行,每行包含一些<select >和<input>字段,这些字段具有onchange处理程序,这些处理程序包含包含tr的id,如

<select id="System_row_0_environment" class="fixedwidth" 
  onchange="changeAccessSystem('System_row_0', 'environment')" name="System_environment">

我想克隆其中一行,并更新ID和对onchange处理程序的调用。 (idGlob是一个全局变量,具有行数的计数)

function cloneLine(previd) {
  var id = '<% $prefix %>_row_' + idGlob;
  idGlob++;

  var $prevLine = jQuery('#' + previd);

  var prevId = $prevLine.attr('id');

  var regExp = new RegExp(prevId, 'g');

  var replaceIdFunction = function(row, attr) {
    if (attr) {
      return attr.replace(regExp, id);
    }
  };

  var $newLine = $prevLine.clone();
  $newLine.attr('id', id).find('*').each(function(index, element) {
     jQuery(element).attr(
     {
        'id': replaceIdFunction,
        'onchange' : replaceIdFunction,
        'for' : replaceIdFunction,
        'onclick' : replaceIdFunction
     })
  });

  // XXX This is a work-around for a bug in Firefox.  Clone is supposed to
  // copy the value, but it doesnt for select and textarea!
  // https://bugzilla.mozilla.org/show_bug.cgi?id=230307
  $prevLine.
    find('select,textarea').each(function(index, element) {
        var $element = jQuery(element);
        var name = $element.attr('name');
        $newLine.find('[name="' + name + '"]').val(
            $element.val());
    });

  $prevLine.after($newLine);
}

这在所有常见的可疑对象(Chrome,Firefox,Safari)中都可以很好地工作,但是由于某些奇怪的原因,即使在IE上也可以在IE上使用,即使您使用Firebug Lite检查了该元素,它也显示了具有onchange="changeAccessSystem('System_row_1', 'environment') ,则在更改它时,将以第一个参数为“ System_row_0”的方式调用.clone ,无论用true还是false调用.clone似乎都没有关系。

我之前在IE上遇到过这种麻烦。 我找到2个解决方案。 首先,从您选择的内联“ onchange”事件中删除该函数。 而是使用jQuery / JavaScript添加事件。 如:

$("select").change(function(e) { // do work ....

其次,在较旧的IE中,change / onchange事件无法正常启动。 因此,您必须对“ propertychange”事件有所了解。 像这样:

$('select').bind($.browser.msie ? 'propertychange': 'change', function(e) { // do work

当然,您可以随后在事件函数中调用函数,并根据哪个元素正在调用绑定事件来根据需要设置参数。 如果需要特定计数或获取特定行号的父tr索引等,也可以使用.each。

希望这会有所帮助,如果需要更多示例,请点击此帖子发表评论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM