【发布时间】:2015-06-26 08:24:13
【问题描述】:
我正在尝试通过单击 元素并将它们添加到数组中来附加城市名称,但我被卡住了,这就是我到目前为止所得到的:
<div id='cities'>pick cities:
<div id='csffs'></div>
<ul>
<li>San francisco</li>
<li>Palo Alto</li>
<li>San Diego</li>
<li>Los Angeles</li>
<li>Toledo</li>
</ul>
</div>
$('li').on('mouseup', function() {
var city = $(this).text();
var bunch_of_cities = ['New York','Cleveland','Port Clinton','Toledo'];
if($.inArray(city,bunch_of_cities) > -1){
//if found, not added up to the array
city = null;
}else{
// if not found, then add it up to the array
bunch_of_cities.push(city);
}
$("#csffs").html(free_to.join());
});
我面临的问题是我需要能够添加超过 1 个城市,而不是像我上面留下的演示那样:
即:
New York,Cleveland,Port Clinton,Toledo [Whichever city was clicked]
我想要这样的东西:
New York,Cleveland,Port Clinton,Toledo,San francisco, Palo Alto,....so forth and so on
请随意更改位或给我一个更好的方法来做到这一点,当然感谢您的时间!!!
PD:我一直在学习自己编码,所以请原谅上面的草率代码
【问题讨论】:
-
在全局范围内声明
bunch..数组。
标签: jquery arrays function push