jQuery - 使用每个对象创建一个对象数组
我是 jQuery 新手。
我有一个包含 n 行的简单表单(尽管我没有使用 html 表单):
<div id="myCities">
<div class="line">City1: <input type="text" /></div>
<div class="line">City2: <input type="text" /></div>
<div class="line">City3: <input type="text" /></div>
<button>Add Your Cities</button>
</div>
我有一个名为“users”的 javascript var,其中包含一般用户数据:
var users = [
{ "username": "John", "year": 1999},
more users...
]
单击按钮时,我想向用户添加城市数组数据(假设我们正在与约翰合作,所以他是[0])
我希望该对象看起来像:
{ "username": "John",
"year": 1999,
"cities": [
{ "City1": $('.line input).val() },
... and so on for the 3 cities entered
]
}
我尝试使用
$.each($('.line'), function() {
// but I'm not really sure what to put here
});
谢谢!
I'm a jQuery newbie.
I have a simple form with n lines (although I'm not using html form):
<div id="myCities">
<div class="line">City1: <input type="text" /></div>
<div class="line">City2: <input type="text" /></div>
<div class="line">City3: <input type="text" /></div>
<button>Add Your Cities</button>
</div>
I have a javascript var called "users" with general users data:
var users = [
{ "username": "John", "year": 1999},
more users...
]
When clicking on the button, I want to add an array of cities to the user's data (let's say we are working with John so he's [0])
I want the object to look like:
{ "username": "John",
"year": 1999,
"cities": [
{ "City1": $('.line input).val() },
... and so on for the 3 cities entered
]
}
I tried using
$.each($('.line'), function() {
// but I'm not really sure what to put here
});
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this