发生“ +”号转换

时间:2019-04-15 15:35:03

标签: javascript php

“ +”登录路径被转换为空格

下面的代码片段可以正常工作,直到我的文件路径包含“ +”符号为止,随后,当它到达Download.php时,它已转换为空格

    $('#ViewPdf').on('click', function() {
        var file = $('#PdfPath').val();
        if(file) {
            $(location).prop('href', 'Download.php?DeleteTarget&download_file='+file);
        }
        else { alert('No pdf file so cannot view'); }
    });

我不确定问题是JavaScript还是php。某种形式的转义或字符转换看起来是必要的,但我不确定。

2 个答案:

答案 0 :(得分:3)

尝试使用encodeURIComponent()

var file = encodeURIComponent($('#PdfPath').val());

答案 1 :(得分:2)

您可以在变量file初始化时使用encodeURIComponent

就像您将+符号编码为%2B

var file = encodeURIComponent($('#PdfPath').val());