我的表单上有一个包含一行或多行的表,每行包含一些<select
&gt;和<input>
字段,其中包含包含包含tr
标识的onchange处理程序,如
<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上,即使您使用Firebug Lite检查元素并且它显示具有onchange="changeAccessSystem('System_row_1', 'environment')
的克隆,当您更改时它,使用第一个参数'System_row_0'调用changeAccessSystem函数。我是否使用.clone
或true
拨打false
似乎无关紧要。
答案 0 :(得分:1)
在我之前遇到IE有点麻烦。找到2个解决方案。首先,从您选择的内联“onchange”事件中删除该函数。相反,使用jQuery / JavaScript添加事件。如:
$("select").change(function(e) { // do work ....
其次,在较旧的IE中,更改/ onchange事件无法正常触发。因此,你必须通过“propertychange”事件变得狡猾。像这样:
$('select').bind($.browser.msie ? 'propertychange': 'change', function(e) { // do work
当然,您可以在事件函数中调用您的函数,并根据调用绑定事件的元素根据需要设置参数。您也可以使用.each,如果您想要特定的计数或获取特定行号的父tr索引,等等....
希望这有帮助,如果需要更多示例,只需在评论中点击此帖子