Javascript 在Javascript中用逗号(,)分隔的拆分项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8401277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split items separated by commas (,) in Javascript
提问by Tommy Dubé-Leblanc
My Javascript var contains a 2D array. If I pop an alert on the the var i get the JSON serialized result, something like:
我的 Javascript var 包含一个二维数组。如果我在 var 上弹出警报,我会得到 JSON 序列化结果,例如:
ID0, DESCRIPTION
I'd like to get each items separated by the , in the value option of the dropdownlist and the other item in the description.
我想在下拉列表的值选项和描述中的另一个项目中用 , 分隔每个项目。
Here's my Javascript code, it would work if split was working correctly but this pops an error because the var doesn't contain a pure string type.
这是我的 Javascript 代码,如果 split 工作正常,它将起作用,但这会弹出一个错误,因为 var 不包含纯字符串类型。
$.ajax(
{
type: "POST",
url: "Projet.aspx/GetDir",
data: "{VP:'" + dd_effort_vp + "',DP:'" + dd_effort_dp + "',Direction:'" + dd_effort_d + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var cars = response.d;
$.each(cars, function(index, value) {
$('#<%= dd_effort_directionp.clientid()%>').append(
$('<option</option>').val(value[value.split(",",0)]).html(value.split(",",1))
}
}
});
I know split doesn't work that way here because of the return value is not a string but you get the result i'd like to achieve, get the first value before the comma has the VALUE of the Dropdownlist and the item after the comma as the HTML text.
我知道 split 在这里不起作用,因为返回值不是字符串,但是您得到了我想要实现的结果,在逗号具有 Dropdownlist 的 VALUE 和逗号之后的项目之前获取第一个值作为 HTML 文本。
Thanks ALOT!
多谢!
回答by codeling
How about value.split(",")[0]
instead of value.split(",",0)
?
value.split(",")[0]
代替怎么样value.split(",",0)
?
回答by IronicMuffin
Have you tried value.toString().split(",")
?
你试过value.toString().split(",")
吗?