2013-12-18 79 views
0

我試圖增加循環函數中使用的字母,每次用戶選擇另一個可能的終點時都會調用此函數。然而,它繼續使用名稱B,有沒有辦法每次增加它?在javascript中遞增字母

var tempEnd = String.fromCharCode('A'.charCodeAt(0) + 1); 

/* create end point */ 
function end_point(t) { 
    calculate_start = false; 
    name = tempEnd; 
    marker = new nokia.maps.map.StandardMarker(coordinate, { 
     text: name, 
     draggable: true, 
     e_num: end_ar.length 
    }); 
    marker.addListener("drag", dragAndDrop, true); 
    marker.addListener("dragend", dragAndDrop, false); 
    document.getElementById("points").style.display = 'none'; 
    map.objects.add(marker); 
    if (t == true) { 
     map.zoomTo(map.getBoundingBox()); 
    } 
    end_ar.push(coordinate); 
    createTable(); 
}; 

感謝

回答

2

只是

var tempEnd='A'; 
tempEnd = String.fromCharCode(tempend.charCodeAt(0) + 1); 
+0

這給了我相同的結果 – Owen

+0

那麼你必須運行'te mpEnd = String.fromCharCode(tempend.charCodeAt(0)+ 1);'每次你想遞增 – scrblnrd3

+0

對,謝謝 – Owen

0

好像你要使用tempEnd遞增代替它,在這種情況下,你可能想要每次在外部存儲的名稱,然後調用它增量:

//store name 
var name = "A" 
//create tempEnd incrementation 
function tempEnd(str){ 
    var number = 1; 
    letter = String.fromCharCode(str.charCodeAt(0) + number); 
    number++; 
    return letter; 
} 

//call name on tempEnd 
name=tempEnd(name); 
name=tempEnd(name); 
//name is now "C"