我正在尝试实现一个自动完成的html输入字段,该字段从sql数据库中获取一个名称列表,一旦点击输入字段就应该填充这些名称。我遇到了一个使用自己的数组的javascript自动完成代码,但我想用sql数组列表填充它的数组。下面的代码会起作用吗?
<div class="autocomplete">
<input type="text" name="customer" id="myInput" placeholder="Search Customers" value="<?php echo $CustomerSet['id']; ?>">
</div>
<script>
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
var customers = ["<?php
foreach( $this->fetchCustomers[2] as $CustomerSet ) :
echo '<option value="'. $CustomerSet['id'] .'" ';
if ( $_POST['customer'] == $CustomerSet['id'] ) :
echo ' SELECTED ';
endif;
echo '>'. $CustomerSet['id'] .' - '. strtoupper( $CustomerSet['customer_first'] .' '. $CustomerSet['customer_last'] ) .'</option>';
endforeach;
?>"];
autocomplete(document.getElementById("myInput"), customers);
</script>
看起来选项形成为单个字符串。尝试将php中的结果重建为一个简单的数组,然后使用join在一个简单的数组中列出。
<div class="autocomplete">
<input type="text" name="customer" id="myInput" placeholder="Search Customers" value="<?php echo $CustomerSet['id']; ?>">
</div>
<script>
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
<?php
$customers = array();
foreach( $this->fetchCustomers[2] as $CustomerSet ) {
$selected = $_POST['customer'] == $CustomerSet['id'] ? 'SELECTED':'';
$customers[] = '"<option value=\''. addslashes($CustomerSet['id']) .'\' ' . $selected . '>'. addslashes($CustomerSet['id']) .' - '. addslashes(strtoupper($CustomerSet['customer_first'])) .' '. addslashes($CustomerSet['customer_last']) .'</option>"';
}
?>
var customers = [<?php echo join(',', $customers) ?>];
autocomplete(document.getElementById("myInput"), customers);
</script>
我找到了使用jquery的完美解决方案,这似乎已经被其他人在stackoverflow上使用了。我使用的解决方案是https://harvesthq.github.io/chosen/,我所做的只是对我的php / html表单代码做了一些小改动。这对我有很大帮助。