简体   繁体   English

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

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

I have a table on my form that has one or more rows, each one of which contains some <select > and <input> fields which have onchange handlers that contain the id of the containing tr , as in 我的表单上有一个表,该表具有一行或多行,每行包含一些<select >和<input>字段,这些字段具有onchange处理程序,这些处理程序包含包含tr的id,如

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

I want to clone one of the rows, and update the ids and the calls to the onchange handlers. 我想克隆其中一行,并更新ID和对onchange处理程序的调用。 (idGlob is a global variable with the count of the number of rows) (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);
}

This works nicely in all the usual suspects (Chrome, Firefox, Safari) but on IE for some odd reason, even if you inspect the element with Firebug Lite and it shows the clone having onchange="changeAccessSystem('System_row_1', 'environment') , when you change it, the changeAccessSystem function gets called with the first argument being 'System_row_0'. It doesn't seem to matter whether I call .clone with true or false either. 这在所有常见的可疑对象(Chrome,Firefox,Safari)中都可以很好地工作,但是由于某些奇怪的原因,即使在IE上也可以在IE上使用,即使您使用Firebug Lite检查了该元素,它也显示了具有onchange="changeAccessSystem('System_row_1', 'environment') ,则在更改它时,将以第一个参数为“ System_row_0”的方式调用.clone ,无论用true还是false调用.clone似乎都没有关系。

Had this kinda trouble with IE before myself. 我之前在IE上遇到过这种麻烦。 2 solutions I found. 我找到2个解决方案。 First, remove the function from the inline "onchange" event on you selects. 首先,从您选择的内联“ onchange”事件中删除该函数。 Instead, use jQuery/JavaScript to add the event. 而是使用jQuery / JavaScript添加事件。 Such as: 如:

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

Second, in older IE's, the change/onchange event doesn't fire properly. 其次,在较旧的IE中,change / onchange事件无法正常启动。 Thus you have to get crafty with the "propertychange" event. 因此,您必须对“ propertychange”事件有所了解。 Like so: 像这样:

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

Of course, you can then just call your function in the event function and set parameters as needed based on which element is calling the binded event. 当然,您可以随后在事件函数中调用函数,并根据哪个元素正在调用绑定事件来根据需要设置参数。 You could also use .each, if you wanted a specific count or grab parent tr index for specific row number, or etc .... 如果需要特定计数或获取特定行号的父tr索引等,也可以使用.each。

Hope this helps, if need more example, just hit this post in comment 希望这会有所帮助,如果需要更多示例,请点击此帖子发表评论

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

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