我想在動態添加的行上自動完成輸入字段。但它會拋出一個錯誤Uncaught TypeError: $(...).autocomplete is not a function
。jQuery如何動態添加行時使用自動完成功能
參考:http://jsfiddle.net/r08m8vvy/2/
HTML
<div class="section-content trip-content" >
<div class="row">
<form class="cn-form form-horizontal " onsubmit="return false" role="form" data-listing-id="">
<input type="hidden" name="trip_id" value="">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 trip-details">
<div class="col-md-4 col-xs-12 trip-details-mobile">
<input type="text" name="" placeholder="Trip ID" class="form-control consigner-trip-id" id="consignerTripDetails" autofocus>
</div>
<div class="col-md-4 col-xs-12 trip-details-mobile">
<input type="text" name="vehicle_no" placeholder="Vehicle No" class="form-control" readonly>
</div>
<div class="col-md-4 col-xs-12 trip-details-mobile">
<input type="text" name="load_type" placeholder="Load Type" class="form-control" readonly>
</div>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 trip-details">
<div class="col-md-12 col-xs-12 trip-details-mobile">
<textarea type="text" name="loading_supervisor" placeholder="Loading Supervisor" class="form-control" readonly></textarea>
</div>
</div>
</form>
</div>
</div>
jQuery的
$("#addTripRows").click(function (e) {
e.preventDefault();
var tripDetailHTML = '<div class="row"><form class="cn-form form-horizontal" onsubmit="return false" role="form" data-listing-id=""><input type="hidden" name="trip_id" value=""><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 trip-details"><div class="col-md-4 col-xs-12 trip-details-mobile"><input type="text" name="" placeholder="Trip ID" class="form-control consigner-trip-id" id="consignerTripDetails" autofocus></div><div class="col-md-4 col-xs-12 trip-details-mobile"><input type="text" name="vehicle_no" placeholder="Vehicle No" class="form-control" readonly></div><div class="col-md-4 col-xs-12 trip-details-mobile"><input type="text" name="load_type" placeholder="Load Type" class="form-control" readonly></div></div><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 trip-details"><div class="col-md-12 col-xs-12 trip-details-mobile"><textarea type="text" name="loading_supervisor" placeholder="Loading Supervisor" class="form-control" readonly></textarea></div></div></form><button class="remove_field v2-button">Remove</button></div>';
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append(tripDetailHTML);
$("input[id="+ x +"]").autocomplete({
source: availableAttributes
});
}
});
$(wrapper).on("click",".remove_field", function(e){
e.preventDefault();
$(this).parent('div').remove();
x--;
});
任何幫助將是巨大的。提前致謝。
請添加您的html標記,包括腳本引用。很可能你在JQuery.js之前引用了JQuery-UI.js –
你的小提琴完美工作。 –
@ Alexandru-IonutMihai是啊,我用什麼小提琴作爲參考。 –