[英]How do i show a list in a javascript array from a php code using a html input field?
我正在嘗試實現一個自動完成的 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 表單代碼進行了微小的更改。 這在很大程度上幫助了我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.