I have checkboxes on page and i want to POST only checked values onchange each checkbox
$(function () {
$('.list input').change(function (e) {
//e.preventDefault();
var favorite = [];
$.each($(".list input[type='checkbox']:checked"), function () {
favorite[$(this).attr("name")].push = $(this).val();
});
var str;
str = $.param(favorite);
$.ajax({
url: '/schema.asp',
type: 'POST',
data: str,
dataType: 'text',
success: function (response) {
alert(response);
}
});
});
});
But I cant do right syntax to push checked to array
$.each($(".list input[type='checkbox']:checked"), function () {
favorite[$(this).attr("name")].push = $(this).val();
});
Please show the right way.
$(this).attr("name") can vary from (Make[],Model[],Year()) and must be a string
SOLVED:
As nobody answer with full answer, this is finall working code
$(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: '/schema.asp',
type: 'POST',
data: str,
dataType: 'text',
success: function (response) {
alert(response);
}
});
});
});