Friday 15 March 2013

wordpress - jQuery Autocomplete input clears itself -



wordpress - jQuery Autocomplete input clears itself -

i using jquery autocomplete in wordpress custom post. when filling out autocomplete input field, ajax seems work correctly, , returns matching data. 1 time select item in drop-down list though, input field clears itself. walked through javascript step step, , input field #title correctly set populateform() function @ point after script has finished, field emptied.

does know might causing this, or of decent way work around it?

i've included js below. allow me know if there's other info might help.

(function ($) { var url = ajax_object.ajaxurl; var popped = [] $('#title') .autocomplete({ source: function( request, response ) { $.getjson( url, { term : extractlast( request.term ), action : 'autocomplete' }, response); }, search: function() { var term = extractlast( this.value ); if ( term.length < 2 ) { homecoming false; } }, select: function( event, ui ) { populateform(ui.item); }, response: function( event, ui ) { (var in ui.content) { popped.push(ui.content[i]); } } }).data("ui-autocomplete")._renderitem = function(ul, item) { homecoming $("<li></li>") .data("ui-autocomplete-item", item) .append("<a><strong>" + item.name + "</strong> (" + item.username.replace(/[^a-za-z]+/g, '') + ")</a>") .appendto(ul); }; function populateform(obj) { $('input#title').val(obj['username']); (var property in obj) { if (obj.hasownproperty(property)) { $('input#form-' + property).val(obj[property]); } } } function split( val ) { homecoming val.split( /,\s*/ ); } function extractlast( term ) { homecoming split( term ).pop(); } })(jquery);

per autocomplete docs, select event's ui.item has 2 properties: label , value. passing populateform() , trying alter text input's value ui.item.username. if open browser's dev console should see error.

jquery wordpress jquery-autocomplete

No comments:

Post a Comment