在 Javascript 中用逗号 (,) 分隔项目
我的 Javascript var 包含一个二维数组。 如果我在 var 上弹出警报,我会得到 JSON 序列化结果,如下所示:
ID0, DESCRIPTION
我想在下拉列表的值选项和描述中的其他项目中获取由 , 分隔的每个项目。
这是我的 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))
}
}
});
我知道 split 在这里不起作用,因为返回值不是字符串,但您得到了我想要实现的结果,在逗号之前获取第一个值,其中包含 Dropdownlist 的 VALUE 和逗号之后的项目作为 HTML 文本。
多谢!
My Javascript var contains a 2D array.
If I pop an alert on the the var i get the JSON serialized result, something like:
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.
$.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.
Thanks ALOT!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用
value.split(",")[0]
代替value.split(",",0)
怎么样?How about
value.split(",")[0]
instead ofvalue.split(",",0)
?您尝试过 value.toString().split(",") 吗?
Have you tried
value.toString().split(",")
?