js object problem
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()
{
开发者_Go百科 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.
In order to have more than one value, arr
's property c
will need to be an array:
arr = {a:["1"], b:["1"], c:["1","2"]}; //that's my object rule
Of course, your validity check must also change to search the new array:
typeof arr[k] !== 'undefined' && contains(arr[k], arr2[k])
...
function contains(a, obj){
for(var i = 0; i < a.length; i++) {
if(a[i] === obj){
return true;
}
}
return false;
}
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 to arr = {a:"1", b:"1", c:"2"};
letting the last definition of c overwrite the first one
精彩评论