I have an array like
var favorite = {};
$.each($(".list input[type='checkbox']:checked"), function(){
if(typeof(favorite[$(this).attr("name")]) == 'undefined'){
favorite[$(this).attr("name")] = [];
}
favorite[$(this).attr("name")].push($(this).val());
});
With one dimensional array I can do:
$('#list [value="'+favorite.join('"],[value="')+'"]').prop('checked',true);
Which produces a selector like:
$('#list [value="1"],[value="3"],[value="4"],[value="5"]')
But how can I produce a selector from two dimensional array like:
$('#list [Name="Name[]",value="1"],[Name="Name[]",value="3"],[Name="Name[]",value="4"],[Name="Model[]",value="5"]')
I need to select all checkboxes which is in favorite array. Because after ajax Post I lose all checked checkboxes something like that:
$(function() {
$('.list input').change(function(e){
//e.preventDefault();
var favorite = {};
$.each($(".list input[type='checkbox']:checked"), function(){
if(typeof(favorite[$(this).attr("name")]) == 'undefined'){
favorite[$(this).attr("name")] = [];
}
favorite[$(this).attr("name")].push($(this).val());
});
var str;
str = $.param(favorite);
$.ajax({
url:'/Search.asp',
type:'POST',
data:str,
dataType:'text',
success: function(data)
{
$("#ExSearchForm").html(data);
$("#ExSearchForm").find('[value=' + favorite.join('], [value=') + ']').prop("checked", true);
}
});
});
});
The Html markup
<div class="list">
<div class="nomination">Make</div>
<div class="name">
<label class='selected-car'><input type='checkbox' name='Make[]' value='*FAKE*' /><span>*FAKE*<i>0</i></span></label>
<label class='selected-car'><input type='checkbox' name='Make[]' value='AIXAM' /><span>AIXAM<i>2</i></span></label>
<label class='selected-car'><input type='checkbox' name='Make[]' value='ALFA ROMEO' /><span>ALFA ROMEO<i>106</i></span></label>
<label class='selected-car'><input type='checkbox' name='Make[]' value='ALPINA' /><span>ALPINA<i>1</i></span></label>
<label class='selected-car'><input type='checkbox' name='Make[]' value='AUDI' /><span>AUDI<i>686</i></span></label>
<label class='selected-car'><input type='checkbox' name='Make[]' value='BMW' /><span>BMW<i>557</i></span></label>
....
$("#ExSearchForm").find(myvar)will not work beause of DOM will be completely updated after POST success. So, references to the elements will be cleared too.