Javascript IF 语句将条件误解为函数
Posted
技术标签:
【中文标题】Javascript IF 语句将条件误解为函数【英文标题】:Javascript IF statement misinterpreting conditions as a function 【发布时间】:2021-09-26 09:42:20 【问题描述】:我一直在尝试使用修改后的自动完成代码,但无论出于何种原因,以下代码都会输出“Uncaught TypeError: arr[i].substr is not a function at htmlInputElement”。而不是简单地按预期运行该功能。让我感到困惑的是,当使用具有有限值的预定义数组时,代码完全按照预期工作,但是当使用从 mysql 数据库中提取的值数组时,它会输出错误。
<form action="Home.html" autocomplete="off">
<div class="autocomplete" style="width:300px;">
<input style="position: relative; top: 20px; left: 20px" class="autotext" id="myInput" type="text" name="primary_input" value="test" placeholder="placeholder">
</div>
<button style="position: relative; top:20px; left: 30px" class="submitButton" type="button">Submit</button>
</form>
</body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if($conn -> connect_error)
die("Connection failed: " . $conn -> connect_error);
$result = mysqli_query($conn, "SELECT field_name FROM table_name");
while($row = mysqli_fetch_assoc($result))
$array[] = $row;
?>
<script>
var mysql_array = <?php echo json_encode($array, JSON_HEX_QUOT); ?>;
console.log(mysql_array);
function autocomplete(inp, arr)
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e)
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) return false;
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++)
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() === val.toUpperCase(inp))
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e)
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
);
a.appendChild(b);
);
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e)
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40)
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
else if (e.keyCode == 38) //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
else if (e.keyCode == 13)
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1)
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
);
function addActive(x)
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
function removeActive(x)
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++)
x[i].classList.remove("autocomplete-active");
function closeAllLists(elmnt)
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
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]);
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e)
closeAllLists(e.target);
);
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
autocomplete(document.getElementById("myInput"), mysql_array);
</script>
</body>
【问题讨论】:
不是 PHP 流利的,但你可以尝试将操作放在 JS 中的模板文字中吗? developer.mozilla.org/en-US/docs/Web/javascript/Reference/…console.log(mysql_array);
究竟记录了什么? (结构明智。您当然可以删除敏感数据。)
@Ivar 我实际上已经解决了这个问题,只是遇到了一个涉及数据的问题,我需要它只有以下 console.log "field_name":"Individual_value 的 Individual_value 部分"
@EthanPrescott 然后选择对象的field_name
?见How can I access and process nested objects, arrays or JSON?
@Ivar 我刚刚和其他一些帮助我的人一起经历过,最后我只需要添加 ["field_name"];到 $row 的末尾使行 $array[] = $row["field_name"];
【参考方案1】:
改变这个:
$result = mysqli_query($conn, "SELECT field_name FROM table_name");
while($row = mysqli_fetch_assoc($result))
$array[] = $row;
到这里
$result = mysqli_query($conn, "SELECT field_name FROM table_name");
while($row = mysqli_fetch_assoc($result))
$array[] = $row["poke_name"];
【讨论】:
以上是关于Javascript IF 语句将条件误解为函数的主要内容,如果未能解决你的问题,请参考以下文章