hel*_*llo 10 arrays ajax jquery push
我正在尝试检查数组中是否有值.如果数组中不存在该值,则应将其添加到数组中,如果该值已存在,则应将其删除.
var selectArr = [];
$('.media-search').mouseenter(function(){
    var $this = $(this);
    $this.toggleClass('highlight');
}).mouseleave(function(){
    var $this = $(this);
    $this.toggleClass('highlight');
}).on('click',function(){
    var dataid = $(this).data('id');
    if(selectArry){ // need to somehow check if value (dataid) exists.
    selectArr.push(dataid); // adds the data into the array
    }else{
    // somehow remove the dataid value if exists in array already
    }
}); 
Guf*_*ffa 30
使用inArray方法寻找一个值,push和splice方法来添加或删除项目:
var idx = $.inArray(dataid, selectArr);
if (idx == -1) {
  selectArr.push(dataid);
} else {
  selectArr.splice(idx, 1);
}
| 归档时间: | 
 | 
| 查看次数: | 22441 次 | 
| 最近记录: |