javascript - Send in one ajax POSt checked checkboxes with selected dropdowns -
checked checkboxes decided hold code
$('.list input[type=checkbox]').on('change', function () { var favorite = {}; $('.list input[type=checkbox]:checked').each(function () { var $el = $(this); var name = $el.attr('name'); if (typeof (favorite[name]) === 'undefined') { favorite[name] = []; } favorite[name].push($el.val()); }); $.ajax({ url: '/search.asp', type: 'post', data: $.param(favorite), datatype: 'text', success: function (data) { $("#exsearchform").html(data) .find('input[type=checkbox]').each(function () { var $el = $(this); var name = $el.attr('name'); var value = $el.attr('value') if (favorite[name] && favorite[name].indexof(value) !== -1) { $el.prop('checked', true); } }); } }); });
now need hold dropdowns values that
$('.list select[option]').on('change', function () { var favorite = {}; $('.list select[option]:selected').each(function () { var $el = $(this); var name = $el.attr('name'); if (typeof (favorite[name]) === 'undefined') { favorite[name] = []; } favorite[name].push($el.val()); }); $.ajax({ url: '/search.asp', type: 'post', data: $.param(favorite), datatype: 'text', success: function (data) { $("#exsearchform").html(data) .find('select[option]').each(function () { var $el = $(this); var name = $el.attr('name'); var value = $el.attr('value') if (favorite[name] && favorite[name].indexof(value) !== -1) { $el.prop('selected', true); } }); } }); });
can combined 1 ajax post?
yes can achieved using common function onvaluechange , attaching listener change event of both checkbox , dropdown:
function onvaluechange() { var favorite = {}; $('.list input[type=checkbox]:checked').each(function () { var $el = $(this); var name = $el.attr('name'); if (typeof (favorite[name]) === 'undefined') { favorite[name] = []; } favorite[name].push($el.val()); }); $('.list select[option]:selected').each(function () { var $el = $(this); var name = $el.attr('name'); if (typeof (favorite[name]) === 'undefined') { favorite[name] = []; } favorite[name].push($el.val()); }); $.ajax({ url: '/search.asp', type: 'post', data: $.param(favorite), datatype: 'text', success: function (data) { $("#exsearchform").html(data) .find('input[type=checkbox]').each(function () { var $el = $(this); var name = $el.attr('name'); var value = $el.attr('value') if (favorite[name] && favorite[name].indexof(value) !== -1) { $el.prop('checked', true); } }); $("#exsearchform").html(data) .find('select[option]').each(function () { var $el = $(this); var name = $el.attr('name'); var value = $el.attr('value') if (favorite[name] && favorite[name].indexof(value) !== -1) { $el.prop('selected', true); } }); } }); }; $('.list input[type=checkbox]').on('change',onvaluechange); $('.list select[option]').on('change', onvaluechange);
Comments
Post a Comment