Javascript jQuery如何删除第一个元素之前的逗号?
我正在使用jQuery push()获取一个数组。但是第一个元素前面有一个逗号。我需要第一个元素不带逗号的数组(其余元素需要逗号)。知道吗 谢谢Javascript jQuery如何删除第一个元素之前的逗号?,javascript,jquery,arrays,push,comma,Javascript,Jquery,Arrays,Push,Comma,我正在使用jQuery push()获取一个数组。但是第一个元素前面有一个逗号。我需要第一个元素不带逗号的数组(其余元素需要逗号)。知道吗 谢谢 function delImg(newThis){ $(newThis).parent().hide(); var Id = $(newThis).siblings('.hidden').html(); Ids.push(Id); alert(Ids); return Ids; } 您正在向数组中添加一个空元
function delImg(newThis){
$(newThis).parent().hide();
var Id = $(newThis).siblings('.hidden').html();
Ids.push(Id);
alert(Ids);
return Ids;
}
您正在向数组中添加一个空元素,以便在开始时获得
,
function delImg(newThis) {
$(newThis).parent().hide();
var Id = $(newThis).siblings('.hidden').html();
if ($.trim(id) !== '') { //push to array if id value is not empty
Ids.push(Id);
}
alert(Ids);
return Ids;
}
您需要.trim()Id的HTML。@writeToBhuwan ty Sir,但我使用的是
$.trim()
。但是第0个元素仍然没有定义。
function delImg(newThis) {
$(newThis).parent().hide();
var Id = $(newThis).siblings('.hidden').html().trim();
if (Id !== '') {
Ids.push(Id);
}
alert(Ids);
return Ids;
}