js对象问题
我使用一个对象来检查一组单选按钮是否具有精确的值,例如在“规则”对象上设置的值。这是一个例子:
arr = {a:"1", b:"1", c:"1", c:"2"}; //that's my object rule
var arr2={}; //here I create my second array with charged value
$("#form_cont input:checked").each(function()
{
arr2[$(this).attr("name")]=$(this).val();
});
//here I make the check
for (k in arr2)
{
if (typeof arr[k] !== 'undefined' && arr[k] === arr2[k])
{
$("#form_cont li[name$='"+k+"']").css('background-color', '');
}
else
{
$("#form_cont li[name$='"+k+"']").css('background-color', 'pink');
}
}
问题是,当我必须检查“c”键时,我得到的最后一个(2)而不是正确的值,如何可能是 e 1 或 2
提前感谢
ciao,h。
I use an object to check that a group of radio buttons have a precise value like set on "rule" object. Here is an example:
arr = {a:"1", b:"1", c:"1", c:"2"}; //that's my object rule
var arr2={}; //here I create my second array with charged value
$("#form_cont input:checked").each(function()
{
arr2[$(this).attr("name")]=$(this).val();
});
//here I make the check
for (k in arr2)
{
if (typeof arr[k] !== 'undefined' && arr[k] === arr2[k])
{
$("#form_cont li[name$='"+k+"']").css('background-color', '');
}
else
{
$("#form_cont li[name$='"+k+"']").css('background-color', 'pink');
}
}
The problem is when I have to check the "c" key I get last the one (2) and not the right value how that may e 1 or 2
thanks in advance
ciao, h.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了拥有多个值,
arr
的属性c
需要是一个数组:当然,您的有效性检查也必须更改以搜索新数组:
...
In order to have more than one value,
arr
's propertyc
will need to be an array:Of course, your validity check must also change to search the new array:
...
一个对象上不能有两个名称相同的属性。因此,当 javascript 编译器看到行
arr = {a:"1", b:"1", c:"1", c:"2"};
时,它会自动将其更改为arr = {a:"1", b:"1", c:"2"};
让 c 的最后一个定义覆盖第一个定义You cannot have two properties on an object that are named the same. Thus when the javascript compiler sees the line
arr = {a:"1", b:"1", c:"1", c:"2"};
it automatically changes it toarr = {a:"1", b:"1", c:"2"};
letting the last definition of c overwrite the first one