javascript - 动态部分搜索匹配正确显示但仍然未捕获 TypeError: Cannot read property 'LID' of undefined at HTMLFormElement
问题描述
当用户在搜索栏中输入时,我正确显示并更新了我传入的数组中的部分匹配项的 javascript 代码。所以我想要的功能都是有效的。
但它只在我的javascript函数中有“console.log(allLIDsCodes [0] ['LID']”时才显示匹配项。如果我有另一个键'代码'的索引号,它也可以工作,但它没有'如果 console.log 像“key”这样的随机字符串,则不起作用。
此外,即使搜索和显示结果正确完成,我仍然在控制台中收到此错误。有任何想法吗?
错误
searchLIDsCodes.js:21 Uncaught TypeError: Cannot read property 'Code' of undefined
at HTMLFormElement.<anonymous> (searchLIDsCodes.js:21)
at HTMLFormElement.dispatch (jquery-1.12.4.js:5226)
at HTMLFormElement.elemData.handle (jquery-1.12.4.js:4878)
(anonymous) @ searchLIDsCodes.js:21
dispatch @ jquery-1.12.4.js:5226
elemData.handle @ jquery-1.12.4.js:4878
chart.php(底部)
<!-- pass session arrays $allFIDs and $allLIDsCodes to javascript -->
<script>
var js_$allFIDs = <?php echo json_encode($_SESSION['allFIDs']); ?>;
var js_$allLIDsCodes = <?php echo json_encode($_SESSION['allLIDsCodes']); ?>;
</script>
<!-- selector for exp fund chart -->
<form id="expLid" method="POST">
<input id="expLidInput" type="text" name="expLid" autofocus="autofocus"></input>
<input type="hidden" name="FID" value="<?php echo $FID; ?>"></input> <!-- $FID must be passed as hidden variable so that it can be appended to url in c_session.php file -->
<button type="submit">Set Exp Lid</button>
<br>
<!-- use this span element to display search results -->
<span id="searchLIDsCodesAppend"></span>
</form>
</div> <!-- page -->
<script>
onload=searchLIDsCodes(js_$allLIDsCodes);
</script>
</body>
</html>
searchLIDsCodes.js
//#region
// http://jsfiddle.net/BeNdErR/f5use/5/
$(document).ready(function () {
document.getElementById("expLidInput").addEventListener("keyup", searchLIDsCodes);
}); // end $(document).ready(function () {
function searchLIDsCodes(allLIDsCodes) {
// start $("#expLid").on("keyup", function() {
$("#expLid").on("keyup", function () {
// *** search results don't display if I just have this
// console.log("key");
// *** search results display for either of these two lines but give the error in the console
// console.log(allLIDsCodes[0]['LID']);
console.log(allLIDsCodes[0]['Code']);
$("#searchLIDsCodesAppend").empty();
var searchValue = $("#expLidInput").val().toLowerCase();
if(searchValue != ""){
for(var x = 0; x < allLIDsCodes.length; x++){
if((allLIDsCodes[x]['LID'].indexOf(searchValue) == 0 ) || (allLIDsCodes[x]['Code'].toLowerCase().indexOf(searchValue) == 0 )){
$("#searchLIDsCodesAppend").append(allLIDsCodes[x]['LID'] + " " + allLIDsCodes[x]['Code'] + "<br>");
} // end if((allLIDsCodes[x]['LID'].indexOf(searchValue) == 0 ) || (allLIDsCodes[x]['Code'].toLowerCase().indexOf(searchValue) == 0 )){
} // end for(var x = 0; x < allLIDsCodes.length; x++){
} // end if(searchValue != ""){
}); // end $("#expLid").on("keyup", function () {
}; // end function searchLIDsCodes(allLIDsCodes) {
//#endregion
解决方案
推荐阅读
- angular - 如何避免 firebaseUI 电子邮件链接身份验证打开第二个浏览器选项卡?
- sql - 根据另一个表中的多行更新表中的行
- java - java二进制到十进制转换器
- python - 列表理解帮助:Python
- spring - 使用 Spring Data 进行复杂查询(使用具有 ManyToOne 关系的 2 个表)
- xmpp - 无法打开 ejabbered Web 仪表板 localhost:5280
- c# - 如何将包含逗号的多值参数传递给 dotnet 命令行工具中的属性?
- node.js - TypeORM 获得多对多关系的一侧
- c# - Outlook 插件 BeforeAttachmentAddEventHandler 中缺少附件类型
- go - 使用不同级别的接口