【问题标题】:How to remove spaces from a query string using Jquery?如何使用 Jquery 从查询字符串中删除空格?
【发布时间】:2013-06-30 22:51:44
【问题描述】:

我在 jquery 中使用查询字符串

获取使用的 URL 值

    function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;

在这里,当我将查询字符串传递给 URL 时,它给出的空格为 %20,当我从 URL 中提取值时,我将名称的值作为 Name%20Name 格式

我应该怎么做才能获得以空格分隔的名称??

【问题讨论】:

    标签: jquery query-string querystringparameter


    【解决方案1】:

    您需要decodeURIComponent() Javascript 函数:

    decodeURIComponent("Name%20Name") // "Name Name"
    

    解码以前由 encodeURIComponent 或类似例程创建的统一资源标识符 (URI) 组件。

    欲了解更多信息,请参阅the documentation

    【讨论】:

      【解决方案2】:

      使用原生Javascript函数unescape,各大浏览器都支持:

      var a = "Name%20Name";
      window.unescape(a);
      

      【讨论】:

      • 但是在这里,当我将 a 传递给我的 Json 调用数据时,我没有让我的 [WebMethod] 命中..,教它一个字符串
      【解决方案3】:

      您可以使用decodeURIComponent(); 将任何编码的网址转换为“正常”表示

      function getUrlVars() {
          var vars = [], hash;
          var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
          for (var i = 0; i < hashes.length; i++) {
              hash = hashes[i].split('=');
              vars.push(decodeURIComponent(hash[0]));
              vars[hash[0]] = hash[1];
          }
          return vars;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        • 2011-08-23
        • 2021-09-04
        • 1970-01-01
        • 2020-02-02
        • 2019-10-23
        相关资源
        最近更新 更多