Javascript 双字符串替换问题
我对以下函数中的“第二轮”文本替换存在问题。一切都会执行,没有JS错误等。唯一的问题是,当我对字符串变量执行第二个“替换”函数时,无法替换“模板”字符串中的“^”字符 我尝试了一些不同的测试:只需自己运行'else'语句(相当简单的替换),玩弄条件安排,等等,但我仍然无法将我的'secondary concerties'附加到文本替换中(甚至根本无法替换carrot占位符) 如果有什么帮助的话,我正在使用Shopify JSON API“选项”是GET产品请求的结果,并使用“选项”JSON键及其值Javascript 双字符串替换问题,javascript,json,shopify,Javascript,Json,Shopify,我对以下函数中的“第二轮”文本替换存在问题。一切都会执行,没有JS错误等。唯一的问题是,当我对字符串变量执行第二个“替换”函数时,无法替换“模板”字符串中的“^”字符 我尝试了一些不同的测试:只需自己运行'else'语句(相当简单的替换),玩弄条件安排,等等,但我仍然无法将我的'secondary concerties'附加到文本替换中(甚至根本无法替换carrot占位符) 如果有什么帮助的话,我正在使用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);
}
});
var createConfigsList=函数(选项){
/*我们正在使用的选项和产品选项的容器*/
var container=jQuery(“.ta optioncontainer”);
forEach(函数(选项、索引、值){
/*我们正在使用的选项名称/以及标签和选择下拉列表值*/
var name=option.name;
/*定义选择框和值替换的格式*/
var labelFormat='|';
var selectFormat='';
var selectClose='';
var optionFormat='^';
如果(name!=“Title”){/*“Title”是默认的Shopify变体选项名称*/
/*工作HTML构建变量*/
变量bLabel、b选择、b选项、附录格式;
/*创建标签并选择“开始”框*/
bLabel=labelFormat.replace(“|”,名称);
b选择=选择格式。替换(“|”,名称);
/*此选项集中的值列表*/
var值=option.values;
value.forEach(函数(optVal){
/*为此选项标记定义HTML构建变量*/
var期权;
/*创建选项;替换带有实际值的值占位符*/
singleOption=optionFormat.replace(“|”,optVal);
/*选项标记文本的次要注意事项*/
如果(名称=“长度”){
单选项。替换(“^”,optVal+“长度”);
}
else if(名称=“深度”){
单选项。替换(“^”,optVal+“深度”);
}
否则{
/*如果没有特殊考虑,只需更换*/
单选项。替换(“^”,optVal);
}
/*将选项合并到选项的“列表”中*/
b选项=b选项+单一选项;
});
/*关闭select,然后将新区域附加到显示容器中*/
appendFormat=bLabel+b选择+b选项+selectClose;
container.append(appendFormat);
}
});
您需要将replace()
的结果分配回变量
singleOption = singleOption.replace("^", optval + " Length");
所有其他的替换电话也是如此。天啊,我真是个笨蛋。谢谢你。