最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Split items separated by commas (,) in Javascript - Stack Overflow

programmeradmin4浏览0评论

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 ma has the VALUE of the Dropdownlist and the item after the ma as the HTML text.

Thanks ALOT!

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 ma has the VALUE of the Dropdownlist and the item after the ma as the HTML text.

Thanks ALOT!

Share Improve this question asked Dec 6, 2011 at 14:13 Tommy Dubé-LeblancTommy Dubé-Leblanc 3171 gold badge7 silver badges20 bronze badges 1
  • So is your problem solved now? If yes, please accpet the answer which helped you most. Or if not, could you provide us with a little more data? – codeling Commented Dec 7, 2011 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 6

How about value.split(",")[0] instead of value.split(",",0)?

Have you tried value.toString().split(",")?

发布评论

评论列表(0)

  1. 暂无评论