开发者

Why is this code generating extra commas?

I am using javascript

function chkout_pp(i) {
    var myarr开发者_StackOverflow中文版ay = new Array();
    var li = 1;
    myarray[0] = ""
    for(j = 1; j < 13; j++)
    {
        if ($('#chkpp'+j).is(':checked') == true) {  
            myarray[li] = $('#chkpp'+j).val()+"<br>";
            li++;
        }
    } 
    $("#ownerarray").val(myarray);
    $("#edmt_pp").html(myarray+"");
}

This code is generating commas. I want to remove the commas. Is there anyone that can answer my question?


There are commas being generated in this code.

$("#edmt_pp").html(myarray + "");

There's an implicit myarray.toString() here, which will join the array elements together with commas.

To avoid that, do:

$("#edmt_pp").html(myarray.join(""));


It is very difficult to understand what you are asking, but I believe the problem is that you are using an array, so when you do

 $("#edmt_pp").html(myarray+"");

JS converts it to a string, hence putting commas in between the elements.

Use a string instead:

function chkout_pp(i) {
    var mystring = "";
    var li = 1;

    for(j = 1; j < 13; j++)
    {
        if ($('#chkpp'+j).is(':checked') == true) {  
            mystring = mystring + $('#chkpp'+j).val()+"<br>";
            li++;
        }
    } 
    $("#ownerarray").val(mystring);
    $("#edmt_pp").html(mystring);
}


Ohh I feel stupid now. The reason it is returning commas is most likely because $("#edmt_pp").html(myarray+""); is coercing a type conversion from your array variable to a string (which incidentally prints out each element separated by a comma).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜