【问题标题】:Display Javascript variable with quotes in显示带有引号的 Javascript 变量
【发布时间】:2020-12-04 11:08:16
【问题描述】:

我有一个变量,它是重复列表的一部分,当我使用 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>

变量名为 tempDesc 并保存字符串 48" 监视器,并在 console.log 语句中显示。但是当它到达 value="'+tempDesc'" 时,它只显示单词 48。我之前尝试过添加斜杠引用但没有喜悦。

【问题讨论】:

    标签: javascript function loops


    【解决方案1】:

    使用escape() 函数。你可以这样使用它:escape(tempDesc)。这里需要它,因为您必须转义引号才能归档它像普通字符一样进行,而不是像以下那样作为被引用字符串的结尾:

    “这个字符串在“引号”中,但仍然有引号。

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

    【讨论】:

    • 非常感谢,我得到了完整的字符串,但现在有 %22%20。这就是进步。我想我现在可以整理一下了。再次感谢。
    • 虽然这是正确的答案,但如果其他访问此问题的人知道为什么需要转义 tempDesc 会很好。
    • 嗨。发送 tempDesc 的脚本在 PHP 中。我正在编辑一个前同事设置的文件,所以很遗憾我不知道他为什么这样做。对不起,我对此比较陌生。对于其他任何人,为了完成该功能,我使用了 replace() 来替换双引号。 - 即 tempDesc.replace(/%22/g, '&quote');
    猜你喜欢
    • 2014-10-10
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2017-05-21
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多