简体   繁体   English

显示带有引号的 Javascript 变量

[英]Display Javascript variable with quotes in

I have a variable that is part of a repeating list that when I use console.log can see the string with quotes in but when printed chops everything beyond that off.我有一个变量,它是重复列表的一部分,当我使用 console.log 时,可以看到带有引号的字符串,但打印时会将所有内容都砍掉。

function sendToPackList(){
          for(let i = 0; i < $('[name="tempQuant[]"]').length; i++){
              if($('[name="tempQuant[]"]')[i].value > 0){
                packItem++;
                let temprow = $('[name="tempQuant[]"]').eq(i).parent().parent().parent().parent();
                let tempDesc = temprow.find('[name="tempDesc[]"]')[0].value
                console.log(tempDesc);
                let tempWeight = temprow.find('[name="tempWeight[]"]')[0].value
                let tempQuant = temprow.find('[name="tempQuant[]"]')[0].value
                $('#packlistItems').append('<div class="row"><div class="6u"><div style="position:relative">
    <input type="text" name="goodsDesc[]" onkeyup="stockAutocomplete(this)" autocomplete="off" value="'+tempDesc+'"></div></div></div>

variable is named tempDesc and holds string 48" monitor, and displays as that at console.log statement. However when it gets to value="'+tempDesc'" it displays only the word 48. I have tried adding slashes before the quotes but with no joy.变量名为 tempDesc 并保存字符串 48" 监视器,并在 console.log 语句中显示。但是,当它到达 value="'+tempDesc'" 时,它只显示单词 48。我尝试在引号前添加斜杠,但是没有喜悦。

Use the escape() function.使用escape() function。 your could should use it like this: escape(tempDesc) .你应该像这样使用它: escape(tempDesc) It is needed here because you have to escape the quote in order to archive that its proceeded like a normal char and not as the end of the enquoted string like in:这里需要它,因为您必须转义引号才能存档它像普通字符一样进行,而不是像以下那样作为被引用字符串的结尾:

"this String is in "quotes" but still has quotes in it". “这个字符串在“引号”中,但仍然有引号”。

"this String is in \"quotes\" but still has quotes in it".

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM