【问题标题】:how could I append different values into an array?如何将不同的值附加到数组中?
【发布时间】: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());


});

Jsfiddle

我面临的问题是我需要能够添加超过 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


【解决方案1】:

您需要在全局范围内声明初始数组bunch_of_cities。原因是,每次你修改这个数组时,它对所有函数都是一样的。

您的代码用于在每次点击事件时初始化数组。

var bunch_of_cities = ['New York', 'Cleveland', 'Port Clinton', 'Toledo'];
$('li').on('mouseup', function () {
     var city = $(this).text();

     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);
     }
     $("#ok").html(bunch_of_cities.join());
});

另外,我认为你不需要if(...),使用这个

if ($.inArray(city, bunch_of_cities) == -1) {
     // if not found, then add it up to the array
     bunch_of_cities.push(city);
}

Fiddle (基于你的小提琴)

【讨论】:

    【解决方案2】:

    您在 mouseup 函数中声明了 var bunch_of_cities,因此每次调用该函数时都会重新创建它。您必须将其声明为超出该功能的范围。例如

    var bunch_of_cities = ['New York','Cleveland','Port Clinton','Toledo'];
    
    $('li').on('mouseup', function() {
            var city = $(this).text();
        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());
    });
    

    【讨论】:

      【解决方案3】:

      我改变了一点。 您尝试在其中追加数据的数组应该是全局的。

      看看:

      var bunch_of_cities = ['New York', 'Cleveland', 'Port Clinton', 'Toledo'];
      $('li').on('mouseup', function() {
        var city = $(this).text();
        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);
        }
        $("#ok").html(bunch_of_cities.join());
      });
      

      【讨论】:

        猜你喜欢
        • 2018-03-03
        • 2018-07-15
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 2014-03-07
        • 2021-08-21
        • 1970-01-01
        • 2017-08-19
        相关资源
        最近更新 更多