這是創建說明字段的div,第一個字段具有自動完成功能,但另一個問題是我可以動態添加字段,但添加的字段沒有自動完成jQuery。自動完成動態輸入字段失敗
<div id="itemRows" class="form-group">
<label class="col-sm-3 control-label">Immediate Cause</label>
<div class="col-sm-7 btn-group" >
<button onclick="addRow(this.form);" class="btn btn-default dropdown-toggle" style="float:right;" type="button" /><span class="glyphicon glyphicon-plus"></span></button>
<input id="description" type="text" class="form-control" style="margin-bottom:15px; width: 553px;padding: 6px 12px;font-size: 14px;line-height: 1.42857143;color: #000;background-color: #fff;background-image: none;border: 1px solid #ccc;border-radius: 4px;" name="array_causes" placeholder="Immediate Cause">
<input type="hidden" name="ranks" value="1">
<?php echo form_error('immediate_cause'); ?>
</div>
</div>
<?php endif; ?>
<?php if (!empty($result)){
$get_cause_Data = array();
foreach ($result as $row_data) {
$get_cause_Data[] = $row_data->icd.','.$row_data->description;
}
$raw_data = implode('","',$get_cause_Data);
}
?>
<script>
var dropdown = [
"<?php echo $raw_data; ?>"
];
var rowNum = 0;
var rank = 1;
function addRow(frm) {
rowNum ++;
rank ++;
var row = '<div class="form-group" style="margin-left:-6px;" id="rowNum'+rowNum+'"><label class="col-sm-3 control-label">Another Cause</label><div class="col-sm-7 btn-group" ><input type="hidden" name="rank[]" value="'+rank+'"><button type="button" class="btn btn-default dropdown-toggle" style="float:right; margin-right:11px;" onclick="removeRow('+rowNum+');"><span class="glyphicon glyphicon-remove"></span></button><input id="description'+rank+'" type="text" class="form-control" style="width: 553px;padding: 6px 12px;font-size: 14px;line-height: 1.42857143;color: #000;background-color: #fff;background-image: none;border: 1px solid #ccc;border-radius: 4px;" name="array_cause[]" value="'+frm.array_causes.value+'" placeholder="Add another Cause"></div></div>';
jQuery('#itemRows').append(row);
frm.array_cause.value = '';
$("#description"+rank).autocomplete({
source: dropdown
});
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
$("#description").autocomplete({
source: dropdown
});
</script>
不要忘記接受和upvote,如果它適合你.. – 2015-05-11 14:10:56