2017-09-23 61 views
0

我在下面的函數中發生了「第二輪」文本替換問題。一切都執行,沒有JS錯誤等。唯一的問題是,當我對字符串變量執行第二個「替換」函數時,我無法替換「模板」字符串中的「^」字符。雙字符串替換問題

我嘗試了幾個不同的測試:只是自己運行'else'語句(非常簡單的替換),使用條件排列等等,但我仍然無法獲得我的「輔助考慮因素」以追加到我的文本替換(甚至更換胡蘿蔔佔位符)。

如果有任何幫助,我正在使用Shopify JSON API。 「選項」是GET產品請求的結果,並使用「選項」JSON密鑰及其值。

var createConfigsList = function(options) { 

/* Container of options & product option we're working with */ 
var container = jQuery(".ta-OptionsContainer"); 

options.forEach(function(option, index, values) { 
    /* Name of option we're working with/also the label & select dropdown value */ 
    var name = option.name; 

    /* Define formats for select boxes & value replacements */ 

    var labelFormat = '<label>|</label>'; 
    var selectFormat = '<select data-option="|" class="ta-CSelectDrop">'; 
    var selectClose = '</select>'; 
    var optionFormat = '<option data-value="|">^</option>'; 

    if(name != "Title") {  /* 'Title' is default Shopify variant option name */ 
     /* Working HTML building variables */ 
     var bLabel, bSelect, bOption, appendFormat; 

     /* Create the label & select box start */ 

     bLabel = labelFormat.replace("|",name); 
     bSelect = selectFormat.replace("|",name); 

     /* List of values within this option set */ 
     var values = option.values; 

     values.forEach(function(optVal) { 
       /* Define HTML building variable for this option tag */ 
       var singleOption; 

       /* Create option; replace value placeholder w/ actual value */ 

       singleOption = optionFormat.replace("|",optVal); 

       /* Secondary considerations for the text of the option tag */ 

       if(name == "Length") { 
         singleOption.replace("^",optVal + " Length"); 
       } 
       else if(name == "Depth") { 
         singleOption.replace("^",optVal + " Depth"); 
       } 
       else { 
         /* If no special considerations, just do the replacement */ 
         singleOption.replace("^",optVal); 
       } 

       /* Combine the option into the 'list' of options */ 
       bOption = bOption + singleOption; 
     }); 

     /* Close our select, and then append the new area to the display container */ 

     appendFormat = bLabel + bSelect + bOption + selectClose; 

     container.append(appendFormat); 

    } 
}); 

回答

1

您需要的replace()結果分配回變量

singleOption = singleOption.replace("^", optval + " Length"); 

,同樣爲所有其他替代電話。

+0

上帝,我是這樣的塗料。謝謝。 – connormw