【发布时间】:2011-10-12 15:23:10
【问题描述】:
我是 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 users = [
{ "username": "John", "year": 1999},
more users...
]
单击按钮时,我想将一组城市添加到用户数据中(假设我们正在与 John 合作,所以他是 [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
});
谢谢!
【问题讨论】:
-
您想何时将城市列表添加到对象中? John 会单击“保存”或“提交”按钮,还是您需要在他键入时更新它,或者其他什么?
-
点击“添加您的城市”按钮时。没什么特别的。