javascript - 如何使用 html 输入字段从 php 代码显示 javascript 数组中的列表?
问题描述
我正在尝试实现一个自动完成的 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>
推荐阅读
- c# - 如何在视图中使用多个模型/上下文(由 Id 路由)?
- twilio - 在 twilio 函数中从资产读取 csv 文件时遇到问题
- r - R:使用带有“splinefun”和ggplot2“stat_function”的时间序列数据
- python - 在 Python 中生成数字列表及其负数
- python - SQLAlchemy + Marshmallow '未知字段。尽管存在字段,但多对一关系中的错误
- android - 如何停止将 .apk 上传到 BitBucket/Github?
- python - 在任何维度(Python)中生成单位超立方体的功能
- python - 如何在python中添加多变量函数
- python - 为什么 Telepot 发送两条消息而不是一条?
- firebase-cloud-messaging - 在 Android 平台中未收到 IBM MobileFirst 推送通知