双字符串替换问题

问题描述:

我在下面的函数中发生了“第二轮”文本替换问题。一切都执行,没有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); 

    } 
}); 

您需要的replace()结果分配回变量

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

,同样为所有其他替代电话。

+0

上帝,我是这样的涂料。谢谢。 – connormw