I'm using Jquery Autocomplete based on: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/. I am able to select a value from the textbox and add it to a list as in the example. Is it possible to add the selected value to a hidden field? For example <input type='hidden' value='value 1,value 2, value 3' name="SelectedValue" id="SelectedValue"/>
Add a comment
|
3 Answers
To add more than one value to a hidden field:
var hdnValue = $('hdnFieldName').val();
$('hdnFieldName').val(hdnValue +','+selectedValue);
Have a look at string.join as well.
Comments
$('input#suggest').result(function(event, data, formatted) {
$("#SelectedValue").val( $("#SelectedValue").val() + "," data );
});
Look over autocomplete documentation for details
Comments
Don't re-invent the wheel. Just use this piece of jQuery. For each autocomplete input field it creates a hidden field using the original input field's name. In that hidden field the autocompleted key is stored. On submit the keys will be submitted. You don't have to change anything to your backend.
http://www.petefreitag.com/item/756.cfm
$('input.YourClassName').each(function() {
var autoCompelteElement = this;
var formElementName = $(this).attr('name');
var hiddenElementID = formElementName + '_autocomplete_hidden';
/* change name of orig input */
$(this).attr('name', formElementName + '_autocomplete_label');
/* create new hidden input with name of orig input */
$(this).after("<input type=\"hidden\" name=\"" + formElementName + "\" id=\"" + hiddenElementID + "\" />");
$(this).autocomplete({source:'employee-search-json.cfm',
select: function(event, ui) {
var selectedObj = ui.item;
$(autoCompelteElement).val(selectedObj.label);
$('#'+hiddenElementID).val(selectedObj.value);
return false;
}
});
});