In my case, I need to write another field 'id' in hidden input. Therefore, I am adding another field to the data returned from the ajax call.
{label:"Name", value:"Value", id:"1"}
And they added the "Create New" link at the bottom of the list. After clicking the "Create New" button, a modal mod will appear, and you can create a new element there.
$('#vendorName').autocomplete ( { source: "/Vendors/Search", minLength: 2, response: function (event, ui) { ui.content.push ({ label: 'Add a new Name', value: 'Add a new Name' }); }, select: function (event, ui) { $('#vendorId').val(ui.item.id); }, open: function (event, ui) { var createNewVendor = function () { alert("Create new"); } $(".ui-autocomplete").find("a").last().attr('data-toggle', 'modal').addClass('highLight'); $(".ui-autocomplete").find("a").last().attr('href', '#modal-form').addClass('highLight'); } } );
I believe that you can add any additional data field other than βlabelβ and βvalueβ.
I am using bootstrap modal and it could be as follows:
<div id="modal-form" class="modal fade" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"> <div class="row"> </div> </div> </div> </div>
Yang Zhang Jun 15 '15 at 11:56 2015-06-15 11:56
source share