我创建了一个似乎有效的函数,直到我开始向.js文档添加更多函数。
这是html ..
<input id="nameSearch" type="text"/>
<input type="button" value="Search" onclick="search();"/>
这是js ..
function search(){
var bName = document.getElementById("nameSearch").value;
alert(bName);
};
这一直有效,直到我向外部.js文档添加新函数。我还没有在html文件中使用任何这些函数,所以我不确定它们为什么会影响它。
function business(b_name,add_1,add_2,city,state,zip,phone){
this.b_name = b_name,
this.add_1 = add_1,
this.add_2 = add_2,
this.city = city,
this.state = state,
this.zip = zip,
this.phone = phone,
};
var ADW = new business("xxx", "xxx", "xxx", "Tucson", "AZ", "xxx", "xxx-xxx-xxxx");
var PC = new business("xxx", "xxx", "xxx", "Tucson", "AZ", "xxx", "xxx-xxx-xxxx");
var contacts = [ADW, PC];
答案 0 :(得分:3)
这是因为您的business
功能中存在错误。
我相信你正在寻找分号而不是逗号:
function business(b_name,add_1,add_2,city,state,zip,phone){
this.b_name = b_name;
this.add_1 = add_1;
this.add_2 = add_2;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
};
从高层次看,您似乎正在尝试定义对象并使用business
函数作为初始化方法。您可能希望这样做:
let business = {
b_name: b_name,
add_1: add_1,
add_2: add_2,
city: city,
state: state,
zip: zip,
phone: phone
};
Here's some further reading on the topic.
希望这有帮助
答案 1 :(得分:1)
如果你在控制台中查看,你会看到这个错误:
SyntaxError: expected expression, got '}'
它甚至会告诉你哪一行是问题!
您的问题是,您没有使用分号在函数中终止行,您已使用过逗号。
这是正确运行的修复程序:
function business(b_name,add_1,add_2,city,state,zip,phone){
this.b_name = b_name;
this.add_1 = add_1;
this.add_2 = add_2;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
}
var ADW = new business("xxx", "xxx", "xxx", "Tucson", "AZ", "xxx", "xxx-xxx-xxxx");
var PC = new business("xxx", "xxx", "xxx", "Tucson", "AZ", "xxx", "xxx-xxx-xxxx");
var contacts = [ADW, PC];
在a Fiddle,你可以看到它在哪里运行。