Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript代码没有';行不通_Javascript_Function - Fatal编程技术网

JavaScript代码没有';行不通

JavaScript代码没有';行不通,javascript,function,Javascript,Function,HTML代码 <html> <head> <title>Price List </title> </head> <body> <h1> PRICELIST </h1> <form id="formSearch"> <div> <label for="searchBox"> Search products here: </label> <in

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代码

(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",
  }
  ]
},

 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
(function(){//启动匿名函数
变量列表={
“产品清单”:[
{
“名称”:“硬盘”,
“价格”:“50美元”,
“质量”:“好”,
},
{
“名称”:“监视器”,
“价格”:“100美元”,
“质量”:“非常好”,
},
{
“姓名”:“发言者”,
“价格”:“20美元”,
“质量”:“不错”,
},
{
“名称”:“耳机”,
“价格”:“12美元”,
“质量”:“差”,
},
{
“姓名”:“手机”,
“价格”:“300美元”,
“质量”:“优秀”,
},
{
“名称”:“usb存储器”,
“价格”:“30美元”,
“质量”:“最好的”,
}
]
},
target=document.getElementById(“outputPlace”),
searchForm=document.getElementById(“formSearch”),
productList=list.listOfProducts,
listLength=productList.length,
searchValue=document.getElementById(“searchBox”),
searchInput=searchValue.value;
var listMethods={
搜索列表:函数(事件){
event.preventDefault();
var i;
target.innerHTML=“”;
如果(listLength>0&&searchInput!==“”){

对于(i=0;i此对象文字不会在
IE
中飞行,在属性列表的末尾有额外的逗号

var list= {

  "listOfProducts": [

  {
  "name":"hard disk",
  "price": "50$",
  "quality":"good", <-- remove these since there is no property after
  },
  {
  "name":"monitor",
  "price":"100$",
  "quality": "very good", <-- remove these since there is no property after
  },
  //rest of object omitted, still needs changed...
}; <-- end with semicolon
var列表={
“产品清单”:[
{
“名称”:“硬盘”,
“价格”:“50美元”,
“质量”:“好”,
这将在执行时获得
.value
属性,而不是创建指向它的指针。变量
searchInput
将只包含空字符串,这不会改变

将该赋值移动到事件处理程序中,以便在单击按钮时检索该值,该赋值将起作用


(,还修复了@KevinBowersox提到的语法错误)

需要一把小提琴,并解释您希望它做什么以及它正在做什么。欢迎使用堆栈溢出!检查您缺少的一些关键信息,以便我们帮助您解决问题。请不要连续发布同一个问题两次。编辑原始问题。@rlemon:这不是同一个代码,他确实应用了。因为这不是问题所在唯一的错误是,有人建议他再问一个问题。谢谢大家的回答,现在它开始工作了!:)就像莱蒙说的,有人建议我再问一个问题。:)不,他不应该以分号结尾对象文字;这是一个多变量声明。而“This piece is missing”就在他的代码中:
}();//结束匿名函数
(不是我的否决票,其余答案非常有效!)
(function(){
  //code goes in here

})(); <--- This piece is missing;
searchInput=searchValue.value;