我从隐藏的输入中得到了一些 id,比如:
<input type="hidden" id="files" name="files" value="103,104,105">
但是当我尝试将这些 id 添加到一个数组中时,它只是添加了一个字符串:
var id= $(this).parent('.added-files').attr("id");
var beforeIDs = $('#files').val();
if(beforeIDs == ''){
var photosIDs = [];
}else{
var photosIDs = [beforeIDs];
}
console.log(photosIDs);
它的输出:
["103,104,105"]
I just want to like this output
[103,104,105]and with that I can remove easily remove id.
By the way, my whole code is:
var id = $(this).parent('.added-files').attr("id");
val = id.replace('uploaded_file_','');
var beforeIDs = $('#files').val();
if(beforeIDs == ''){
var photosIDs = [];
}else{
var photosIDs = [beforeIDs];
}
console.log(photosIDs);
for(var i = photosIDs.length; i--;) {
if(photosIDs[i] ===val) {
photosIDs.splice(i, 1);
}
}
console.log(photosIDs);
我哪里错了?
谢谢。
最佳答案
您需要split()
,
上的字符串。
这也将为您省去检查空值的步骤。
var beforeIDs = $('#files').val();
var photosIDs = beforeIDs.split(",");
console.log(photosIDs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="hidden" id="files" name="files" value="103,104,105">
关于javascript - 如何从隐藏的输入中获取一些 id 并使用 jquery 或 javascript 对其进行切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768995/