在我的代码中找不到错误? [英] can't find error in my code?

查看:72
本文介绍了在我的代码中找不到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的搜索代码.我找不到错误.错误消息显示Uncaught SyntaxError: Unexpected identifier on javascript line 40 (target=document.getElementById("outputPlace").

I am making a simple search code. I can't find error. The error message says Uncaught SyntaxError: Unexpected identifier on javascript line 40 (target=document.getElementById("outputPlace").

不要看按钮,我还没有添加事件监听器. 我只希望在我按Enter键时显示产品.

Do not look at the button, I have not added event listener to it yet. I just want that when I press enter products are displayed.

HTML代码

<html>
<head>

<title>Price List </title>

</head>

<body>

<h1> PRICELIST </h1>
<form id="formSearch">
<div>
<label for="searchBox"> Search products here: </label>
<input type="text" placeholder="Type text here to search product" id="searchBox">
</div>
<div id="buttons">
<button id="getAll"> GET ALL PRODUCTS</button>
</div>

</form>

<div id="outputPlace">

</div>


<script src="product.js"></script>
</body>


</html>


JAVASCRIPT CODE

(function(){                    //start anonymous function

var list= {

  "listOfProducts": [

  {
  "name":"hard disk",
  "price": "50$",
  "quality":"good",
  },
  {
  "name":"monitor",
  "price":"100$",
  "quality": "very good",
  },
  {
  "name":"speakers",
  "price":"20$",
  "quality": "not bad",
  },
  {
  "name":"headphones",
  "price":"12$",
  "quality":"bad",
  },
  {
  "name": "mobile phone",
  "price": "300$",
  "quality": "excellent",
  },
  {
  "name": "usb memory",
  "price": "30$",
  "quality": "the best",
  }
  ]
},

var target=document.getElementById("outputPlace"),
    searchForm=document.getElementById("formSearch"),
    productList=list.listOfProducts,
    listLength=productList.length,
    searchValue=document.getElementById("searchBox"),
    searchInput=searchValue.value;




var listMethods = {

searchList: function(event) {

event.preventDefault();
var i;
target.innerHTML="";
if(listLength>0 && searchInput!=="") {

   for(i=0;i<listLength;i++) {
   var product=productList[i],
       whatIsFound=product.name.indexOf(searchInput);
       if(whatIsFound!==-1){

       target.innerHTML+='<p>'+product.name+', '+product.price+', '+product.quality+'<a href="http//www.facebook.com">click here to buy</a></p>'
       }

   }


}





},





};

searchForm.addEventListener("submit",listMethods.searchList,false);








}) (); //end anonymous function

推荐答案

在JavaScript顶部定义的大型JSON对象后,您会遇到一个逗号,然后是另一个变量.

You have a comma after that large JSON object you defined at the top of your JavaScript, followed by another var.

var list= {
 "listOfProducts": [
 {
  "name":"hard disk",
  "price": "50$",
  "quality":"good",
 },
 ...[a bunch of stuff]...
},

var target=document.getElementById("outputPlace"),
    searchForm=document.getElementById("formSearch"),
    productList=list.listOfProducts,
    listLength=productList.length,
    searchValue=document.getElementById("searchBox"),
    searchInput=searchValue.value;

另外两个建议的答案都可以解决此问题(好吧,乙女删除了他们的答案,即删除了第二个变量).

Both of the two other proposed answers would fix this (well ok Otome deleted their answer which was to drop the second var).

这篇关于在我的代码中找不到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆