繁体   English   中英

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

[英]function stopped working after adding more functions

在我开始向 .js 文档添加更多函数之前,我创建了一个似乎可以工作的函数。

这是..

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

这是因为您的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
};

这是有关该主题的一些进一步阅读。

希望这可以帮助

如果你查看你的控制台,你会看到这个错误:

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

这是一个 Fiddle ,你可以看到它在运行。

暂无
暂无

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

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