为什么在 jquery 中动态创建的复选框定义属性的顺序会影响其值?
我在 js 文件中有这段代码:
function buildRolePicker() {
var pnl = $("[id$='staffRoles']");
$.each(roles["ContactGroupRoles"], function(iteration, item) {
pnl.append(
$(document.createElement("input")).attr({
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName'],
type: 'checkbox'
})
);
pnl.append(
$(document.createElement('label')).attr({
'for': 'cgr' + item['RoleId']
})
.text(item['RoleName'])
);
});
alert(document.forms[0].cgroles[8].value);
}
我在代码的其他部分浪费了一些时间试图弄清楚为什么调用
alert(document.forms[0].cgroles[8].value);
返回“on”值,而它应该返回 long。事实证明,问题出在属性定义的顺序上。如果我将其更改
$(document.createElement("input")).attr({
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName'],
type: 'checkbox'
})
为
$(document.createElement("input")).attr({
type: 'checkbox',
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName']
})
:一切正常,并且当我:
alert(document.forms[0].cgroles[8].value);
我的问题是为什么时,我会按预期获得长值?
测试数据:
var roles = {"ContactGroupRoles":[
{"RoleId":1,"RoleName":"Pending"},
{"RoleId":2,"RoleName":"CEO"},
{"RoleId":3,"RoleName":"Financial Controller"},
{"RoleId":4,"RoleName":"General Manager"},
{"RoleId":5,"RoleName":"Production Manager"},
{"RoleId":6,"RoleName":"Sales Manager"},
{"RoleId":7,"RoleName":"Marketing Manager"},
{"RoleId":8,"RoleName":"Sales Agent"},
{"RoleId":9,"RoleName":"Customer Service"},
{"RoleId":10,"RoleName":"Manager"}
]};
I have this code in a js file:
function buildRolePicker() {
var pnl = $("[id$='staffRoles']");
$.each(roles["ContactGroupRoles"], function(iteration, item) {
pnl.append(
$(document.createElement("input")).attr({
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName'],
type: 'checkbox'
})
);
pnl.append(
$(document.createElement('label')).attr({
'for': 'cgr' + item['RoleId']
})
.text(item['RoleName'])
);
});
alert(document.forms[0].cgroles[8].value);
}
I was wasting some time in other sections of code trying to work out why a call to
alert(document.forms[0].cgroles[8].value);
was returning a value of "on" when it should be returning a long. It turns out the problem is the order in which the attributes are defined. If i change this:
$(document.createElement("input")).attr({
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName'],
type: 'checkbox'
})
to this:
$(document.createElement("input")).attr({
type: 'checkbox',
id: 'cgr' + item['RoleId'],
name: 'cgroles',
value: item['RoleId'],
title: item['RoleName']
})
everything works fine and I get my long value as expected when i:
alert(document.forms[0].cgroles[8].value);
My question is why?
Test Data:
var roles = {"ContactGroupRoles":[
{"RoleId":1,"RoleName":"Pending"},
{"RoleId":2,"RoleName":"CEO"},
{"RoleId":3,"RoleName":"Financial Controller"},
{"RoleId":4,"RoleName":"General Manager"},
{"RoleId":5,"RoleName":"Production Manager"},
{"RoleId":6,"RoleName":"Sales Manager"},
{"RoleId":7,"RoleName":"Marketing Manager"},
{"RoleId":8,"RoleName":"Sales Agent"},
{"RoleId":9,"RoleName":"Customer Service"},
{"RoleId":10,"RoleName":"Manager"}
]};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎无论出于何种原因,IE(至少是 IE8)和 Opera 都没有通过更改
type
来保留value
属性(尽管 Chrome/Firefox 保留了)。这是一个简化的测试:您可以在此处尝试
IE8/Opera 警报:
Chrome/Firefox 警报:
我不知道为什么Opera 的行为尤其如此,但无论如何......只是试图更好地演示问题,解决方案当然是首先保留
type
属性。也许未来的 jQuery 版本将在循环中首先处理type
,尽管如果之前使用任何属性定义,这仍然不会有太大作用。
然而,
$("", { value: 'Test', type: 'checkbox' });
格式也遇到同样的问题,IMO 这应该 被修复。It seems that for whatever reason, IE (IE8 at least) and Opera don't retain the
value
attribute (though Chrome/Firefox do) through changing thetype
. Here's a simplified test:You can try it here
IE8/Opera alerts:
<input type="checkbox">
Chrome/Firefox alerts:
<input type="checkbox" value="Test">
I'm not sure why Opera in particular is behaving this way, but in any case...just attempting to better demonstrate the issue, the solution of course is to keep the
type
attribute first. Perhaps a future jQuery release will processtype
first in the loop, though if the<input>
was defined with any attributes earlier this still wouldn't do much good.However, the
$("<input />", { value: 'Test', type: 'checkbox' });
format suffers the same problem, IMO this should be fixed.