简体   繁体   English

添加更多功能后功能停止工作

[英]function stopped working after adding more functions

I created a function that seems to work until I start adding more functions to the .js document.在我开始向 .js 文档添加更多函数之前,我创建了一个似乎可以工作的函数。

This is the html..这是..

<input id="nameSearch" type="text"/>
<input type="button" value="Search" onclick="search();"/>

This is the js..这是js..

 function search(){
        var bName = document.getElementById("nameSearch").value;
        alert(bName);
    };

This works until I add a new function to the external .js document.这一直有效,直到我向外部 .js 文档添加一个新函数。 I'm not using any of these functions in the html file yet, so I'm not sure why they would affect it.我还没有在 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];

It's because you have errors in your business function.这是因为您的business功能存在错误。

I believe you're looking for semi-colons instead of commas:我相信您正在寻找分号而不是逗号:

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;
};

From a high level, it looks like you're trying to define an object and using the business function as an initialization method.从高层次上看,您似乎正在尝试定义一个对象并使用business函数作为初始化方法。 You might want to do that instead:你可能想这样做:

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. 这是有关该主题的一些进一步阅读。

Hope this helps希望这可以帮助

If you look in your console, you will see this error:如果你查看你的控制台,你会看到这个错误:

SyntaxError: expected expression, got '}'

It even tells you which line is the problem!它甚至会告诉你哪条线有问题!

Your issue is that you haven't terminated lines within the function with a semi-colon, you've used commas.你的问题是你没有用分号终止函数中的行,你使用了逗号。

Here's the fix, which runs properly:这是修复程序,它可以正常运行:

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];

And here's a Fiddle where you can see it runs.这是一个 Fiddle ,你可以看到它在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM