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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -