在这个jQuery代码中,IE6和IE7有什么地方失败了?
这段jQuery复制了表单中的一个项。它适用于除IE6和IE7之外的所有浏览器。这是因为当它复制表单时,它不会增加name属性(与所有其他浏览器一样):在这个jQuery代码中,IE6和IE7有什么地方失败了?,jquery,html,forms,internet-explorer-7,Jquery,Html,Forms,Internet Explorer 7,这段jQuery复制了表单中的一个项。它适用于除IE6和IE7之外的所有浏览器。这是因为当它复制表单时,它不会增加name属性(与所有其他浏览器一样): 您必须使用outerHTML进行克隆!哎呀 var $this = $(this); if (!$.browser.msie || parseInt($.browser.version) > 7) { $this.attr('id', $this.attr('id').replace(/_(\d+)
您必须使用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 + " />");
};
var$this=$(this);
如果(!$.browser.msie | | parseInt($.browser.version)>7){
$this.attr('id'),$this.attr('id').replace(/\ud+)\u/,函数($0,$1){
返回''+(+$1+1)+'';
}));
$this.attr('name'),$this.attr('name')。替换(/\[(\d+)\]/,函数($0,$1){
返回“['+(+$1+1)+']”;
}));
}否则{
foob_name=$this.attr('name').replace(/\[(\d+)\]/,函数($0,$1){
返回“['+(+$1+1)+']”;
});
foob_id=$this.attr('id'),$this.attr('id')。替换(/\ud+)\u/,函数($0,$1){
返回''+(+$1+1)+'';
}));
$this.attr(“outerHTML”,”);
};
我不明白问题出在哪里。到底是什么失败了?IE6和IE7 HTML中的name属性并没有增加它的数量。之前在@Trips comment之前对name属性所做的评论。。。现在删除:)可能是您在名称属性中使用的无效字符([
和]
有问题。不过我不确定。编辑:我可能错了。它们对ID无效,但我可以使用名称中的代码进行验证。只是不确定。旧IE不允许您更改使用文档创建的元素的“name”属性。createElement()
-当元素作为.cloneNode()
的副产品创建时,可能同样的限制也适用。您可能希望尝试使用本机setAttribute()
而不是.attr()
<代码>this.setAttribute('name',$this.attr('name').replace(/\[(\d+\]/,函数($0,$1){return'['+(+$1+1)+']';})代码>
<DIV class=payment jQuery1297204711741="16">
<INPUT id=payments_0_:date_paid value=3/27/2008 name=payments[0][:date_paid] jQuery1297204711741="10">
<INPUT id=payments_0_:amount_paid value=100 name=payments[0][:amount_paid] jQuery1297204711741="14">
</DIV>
<DIV class=payment style="DISPLAY: block" jQuery1297204711741="27">
<INPUT id=payments_1_:date_paid value=4/2/2008 name=payments[0][:date_paid] jQuery1297204711741="21">
<INPUT id=payments_1_:amount_paid value=100 name=payments[0][:amount_paid] jQuery1297204711741="25">
</DIV>
$(".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;
});
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 + " />");
};