数组在formar“item1,item2”中具有值。我希望它是[“item1”,“item2”]。我该怎么办?

时间:2022-05-27 00:31:35
 console.log(jQuery("#tickets-expanded input:checkbox[name='ids[]']:checked"));
        jQuery("#tickets-expanded input:checkbox[name='ids[]']:checked").each(function() {
           selected.push(jQuery(this).val());
        });
        alert(selected);

        jQuery("#ids").val(selected);

This is my code. Selected array contains elements in the format "2,1". I want it to be ["2","1"]. What should I do?

这是我的代码。 Selected数组包含格式为“2,1”的元素。我希望它是[“2”,“1”]。我该怎么办?

5 个解决方案

#1


1  

Consider the code:

考虑一下代码:

var a = "item1,item2,item3";

a = a.split(",");

console.log( a );

Output will be:

输出将是:

["item1","item2","item3"]

Now you can apply this technique on your code at the end. The string you are getting can be split and you will get the array.

现在,您可以在最后将代码应用于代码。您获得的字符串可以拆分,您将获得该数组。

Edit: You have multiple id elements? So you can also split data among them. Each will contain a value

编辑:你有多个id元素?所以你也可以在它们之间分割数据。每个都包含一个值

#2


1  

Thats what its toString() returns, if you want a different format construct it manually:

这就是它的toString()返回的内容,如果你想要一个不同的格式手动构造它:

str = "[\"" + selected.join("\",\"")​​​​​​​​​​​ + "\"]"​​​​​​;

#3


0  

selected = '[' + selected + ']';

#4


0  

'5,6,7'.split(',')

http://www.w3schools.com/jsref/jsref_split.asp

http://www.w3schools.com/jsref/jsref_split.asp

#5


0  

Try this to get an array of selected checkboxes

试试这个以获得一系列选中的复选框

var values = $("#tickets-expanded input:checkbox[name='ids[]']:checked").map(function () {
  return this.value;
}).get();

#1


1  

Consider the code:

考虑一下代码:

var a = "item1,item2,item3";

a = a.split(",");

console.log( a );

Output will be:

输出将是:

["item1","item2","item3"]

Now you can apply this technique on your code at the end. The string you are getting can be split and you will get the array.

现在,您可以在最后将代码应用于代码。您获得的字符串可以拆分,您将获得该数组。

Edit: You have multiple id elements? So you can also split data among them. Each will contain a value

编辑:你有多个id元素?所以你也可以在它们之间分割数据。每个都包含一个值

#2


1  

Thats what its toString() returns, if you want a different format construct it manually:

这就是它的toString()返回的内容,如果你想要一个不同的格式手动构造它:

str = "[\"" + selected.join("\",\"")​​​​​​​​​​​ + "\"]"​​​​​​;

#3


0  

selected = '[' + selected + ']';

#4


0  

'5,6,7'.split(',')

http://www.w3schools.com/jsref/jsref_split.asp

http://www.w3schools.com/jsref/jsref_split.asp

#5


0  

Try this to get an array of selected checkboxes

试试这个以获得一系列选中的复选框

var values = $("#tickets-expanded input:checkbox[name='ids[]']:checked").map(function () {
  return this.value;
}).get();