【问题标题】:How can I keep the space in text transferred to new page?如何将文本中的空间转移到新页面?
【发布时间】:2013-02-24 19:44:51
【问题描述】:

当用户单击“下一步”按钮时,来自两个单独文本框的数据将被传输到新页面,但有 %20 代替空格。我怎样才能避免这种情况?

来自 originalpage.html 的内联 javascript

$('#next').click(function(){
        var coachFullName = $('#coach_txt_box').val();
        var teamName = $('#team_txt_box').val();
        if(!!coachFullName){
window.location.href = 'newpage.html?zip=' + coachFullName + teamName;
        } else {
            $('# coach_txt_box').watermark('please enter coach name',{className: 'watermarkError'});
        }
    });

newpage.html 中的内联 javascript

function getParameterByName(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if(results == null)
        return "";
    else
        document.signupform.coach_and_team_name.value = results[1];
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

getParameterByName('coach');

【问题讨论】:

  • %20 出现的问题只发生在 Firefox 中。在 IE 中看起来没问题。
  • 通过哪种方法在新页面中获取此传输文本...?
  • @JohnMontague 我认为这是由浏览器自动完成的,因为 URI 中不允许使用空格。浏览器和构建之间可能存在差异...

标签: javascript jquery regex firefox


【解决方案1】:

你应该使用类似的东西 urldecode() 在 php 或 decodeURIComponent() ..

不需要正则表达式来完成你想要的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2021-08-16
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多