issue with radio buttons
-
Appreciate that this is an edge case (as the default search form doesn’t have any radio buttons) but the js that gathers the form content to make the live query doesn’t handle radios correctly; it includes the value from the last radio button, whether checked or not.
Discovered this when I was creating a modified form that had a taxonomy filter on it.
Changing the parameter gathering in
daves-wordpress-live-search.js
(line 225) to the following, fixes the issue:// Marshall the parameters into an object var parameters = {}; var fields = searchBox.parents('form').find('input:not(:submit),select,textarea,input:not(:radio)'); for(fieldIndex in fields) { if(fields.hasOwnProperty(fieldIndex) && /* test for integer */ fieldIndex % 1 === 0) { var field = jQuery(fields[fieldIndex]); parameters[field.attr('name')] = field.val(); } } // handle radio groups cleanly var radioNames = []; searchBox.parents('form').find('input:radio').each(function(radioIndex) { var rName = jQuery(this).attr('name'); if (radioNames.indexOf(rName) == -1) {radioNames.push(rName)} }); for(var radioNameIndex in radioNames) { var radioName = radioNames[radioNameIndex]; var selected = searchBox.parents('form').find("input[type='radio'][name='" + radioName + "']:checked"); if (selected.length > 0) { parameters[radioName] = selected.val(); } }
I can’t see if there’s a cleaner way to do this (for instance, populate a hidden field with the radio value each time one is clicked) but that appeared to add further complexity.
https://www.ads-software.com/plugins/daves-wordpress-live-search/
- The topic ‘issue with radio buttons’ is closed to new replies.