数组Javascript中的对象存在问题
我在一系列对象中搜索时遇到问题。基本上,我的页面需要做的是使用我输入的信息(例如全名、用户名、电子邮件和密码)创建一个新的“客户机”。这些客户机中的每一个都是数组中的对象,如下所示数组Javascript中的对象存在问题,javascript,arrays,Javascript,Arrays,我在一系列对象中搜索时遇到问题。基本上,我的页面需要做的是使用我输入的信息(例如全名、用户名、电子邮件和密码)创建一个新的“客户机”。这些客户机中的每一个都是数组中的对象,如下所示 var clientlist = [{"username":"John","fullname":"John Doe", "email":"john.doe@hotmail.com","type":"client","password":"jdoe2"}, 这个客户端已经在我的js文件中创建了,我需要做的是创建一个新
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","type":"client","password":"jdoe2"},
这个客户端已经在我的js文件中创建了,我需要做的是创建一个新的对象来添加到这个具有相同结构的数组中。比如说,
var clientlist = [{"username":"Peter","fullname":"Peter Jones",
"email":"peter.jones@hotmail.com","type":"client","password":"pjones1"},
我已经编写了代码,但它不能正常工作,当我运行Firebug时,我可以看到所有元素都已正确添加,除了用户名值为“”之外。我似乎无法搜索用户名以查看我添加的用户名是否已经存在,这可能是语法错误。我将在下面留下我的完整代码,并提前感谢您的帮助
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","type":"client","password":"jdoe2"},
var Client = {};
function NewClient(){
var found;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username == user) {
found = true;
}else{
found = false;
}
}
if (found == true){
$("#msj").html("User already exists!");
}
else if(found == false){
Client["fullname"] = $("#fullname").val();
Client["username"] = user;
Client["email"] = $("#email").val();
Client["type"] = "client";
Client["password"] = $("#password").val();
clientlist[clientlist.length] = Client;
$("#msj").html("New client has been created");
}
var clientlist=[{“用户名”:“约翰”,“全名”:“约翰·多伊”,
“电子邮件”:“约翰。doe@hotmail.com,“类型”:“客户端”,“密码”:“jdoe2”},
var客户端={};
函数NewClient(){
var发现;
var user=$(“#用户名”).val();
对于(var i=0;i
}我想你有几个问题
<script>
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","type":"client","password":"jdoe2"}]
function NewClient(){
var found=false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username==user) {
found = true;
break;
}
}
if (found){
$("#msj").html("User already exists!");
}
else{
var newUser={
fullname:$("#fullname").val(),
username:user,
email:$("#email").val(),
type:"client",
password:$("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
</script>
var clientlist=[{“用户名”:“John”,“全名”:“John Doe”,
“电子邮件”:“约翰。doe@hotmail.com“,”类型“:”客户端“,”密码“:”jdoe2“}]
函数NewClient(){
var=false;
var user=$(“#用户名”).val();
对于(var i=0;i
我想你有几个问题
<script>
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","type":"client","password":"jdoe2"}]
function NewClient(){
var found=false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username==user) {
found = true;
break;
}
}
if (found){
$("#msj").html("User already exists!");
}
else{
var newUser={
fullname:$("#fullname").val(),
username:user,
email:$("#email").val(),
type:"client",
password:$("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
</script>
var clientlist=[{“用户名”:“John”,“全名”:“John Doe”,
“电子邮件”:“约翰。doe@hotmail.com“,”类型“:”客户端“,”密码“:”jdoe2“}]
函数NewClient(){
var=false;
var user=$(“#用户名”).val();
对于(var i=0;i
您犯的几个错误:
- 忘记关闭
数组clientlist
- 忘记实际推送新添加的客户端
var clientlist = [{
"username": "John",
"fullname": "John Doe",
"email": "john.doe@hotmail.com",
"type": "client",
"password": "jdoe2"
}];
function NewClient() {
var found = false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username == user) {
found = true;
} else {
found = false;
}
}
if (found) {
$("#msj").html("User already exists!");
} else {
var newUser = {
fullname: $("#fullname").val(),
username: user,
email: $("#email").val(),
type: "client",
password: $("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
var clientlist=[{
“用户名”:“约翰”,
“全名”:“约翰·多伊”,
“电子邮件”:“约翰。doe@hotmail.com",
“类型”:“客户端”,
“密码”:“jdoe2”
}];
函数NewClient(){
var=false;
var user=$(“#用户名”).val();
对于(var i=0;i
为你做了一把小提琴:
你犯的几个错误:
- 忘记关闭
数组clientlist
- 忘记实际推送新添加的客户端
var clientlist = [{
"username": "John",
"fullname": "John Doe",
"email": "john.doe@hotmail.com",
"type": "client",
"password": "jdoe2"
}];
function NewClient() {
var found = false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username == user) {
found = true;
} else {
found = false;
}
}
if (found) {
$("#msj").html("User already exists!");
} else {
var newUser = {
fullname: $("#fullname").val(),
username: user,
email: $("#email").val(),
type: "client",
password: $("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
var clientlist=[{
“用户名”:“约翰”,
“全名”:“约翰·多伊”,
“电子邮件”:“约翰。doe@hotmail.com",
“类型”:“客户端”,
“密码”:“jdoe2”
}];
函数NewClient(){
var=false;
var user=$(“#用户名”).val();
对于(var i=0;i
为你做了一把小提琴:
您在
clientlist
值的末尾缺少]
<代码>var clientlist=[{“用户名”:“约翰”,“全名”:“约翰·多伊”,“电子邮件”:“约翰”。doe@hotmail.com“,”键入“:”客户机“,”密码“:”jdoe2”}]您在我昨天给您的解决方案的循环中使用break
忽略了它。或设置found=false
并删除else