2017-09-23 1 views
0

Je rencontre un problème avec un «second tour» de remplacement de texte dans la fonction suivante. Tout s'exécute, pas d'erreur JS, etc. Le seul problème est que je ne peux pas remplacer le caractère "^" dans ma chaîne 'template' quand je vais faire une seconde fonction 'replace' sur une variable chaîne. J'ai essayé quelques tests différents: exécutez simplement l'instruction 'else' (remplacement assez simple), jouez avec l'arrangement conditionnel, etc., mais je n'arrive toujours pas à ajouter mes 'considérations secondaires' mon remplacement de texte (ou même remplacer l'espace réservé carotte du tout).Chaîne double Remplacer Question

Je travaille avec l'API Shopify JSON si cela peut vous aider. 'options' est le résultat d'une requête de produit GET et utilise la clé JSON 'options' et ses valeurs.

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); 

    } 
}); 

Répondre

1

Vous devez affecter le résultat de replace() retour à la variable

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

et de même pour tous les autres appels remplacer.

+0

Dieu, je suis un tel dope. Je vous remercie. – connormw