有没有办法从encodeURIComponent中排除某些字符

时间:2012-07-12 17:19:06

标签: javascript

我正在为我的网址构建一个查询字符串,需要从编码中排除某些字符。

我想排除“&”和“=”以便我可以这样说:

first = blah& second = blah等等......

我想最好的方法是如何阻止它们被编码?

一些代码:

 else if (array[i].nodeName == "SELECT") {
   if (array[i].id == "multiple") {
     var selected = $.map($('#multiple option:selected'),

     function (e) {
       return $(e).val();
     });
     $.each(selected, function (index, value) {
       name = array[i].name;
       values += app + "\&" + key + "=";
     });

   } else {
     name = arr[i].name;
     values = arr[i].value;
   }
 }

 key = encodeURIComponent(name);
 value = encodeURIComponent(values);
 queryString += name + "=" + values + "&";

2 个答案:

答案 0 :(得分:1)

  

有没有办法从encodeURIComponent排除某些字符?

没有。它是一个内置函数,只需要一个参数。


当它出现在键或值的中间时,您需要对&进行编码,因此最简单的解决方案是在组合它们之前对各个名称和值进行编码。定义

function emit(name, value) {
  queryString += (queryString.indexOf("?") >= 0 ? "&" : "?")
    + encodeURIComponent(name) + "=" + encodeURIComponent(value);
}

然后为多个选择中的每个名称/值对调用该函数,或者为每个其他选中的输入调用一次。

else if (array[i].nodeName=="SELECT" ){
  if(array[i].id == "multiple"){
    var selected = $.map( $('#multiple option:selected'),
                          function(e){return $(e).val();});
    $.each(selected, function(index, value){
             emit(array[i].name, value);
           });
  } else {
    emit(arr[i].name, arr[i].value);
  }
}

使用encodeURI或类似内容无法正确编码#=或其他必要的代码点。

答案 1 :(得分:0)

函数的名称应该建议如何使用它:在查询字符串的上调用它,而不是整个查询字符串。

编辑 - 我试图根据您的代码创建一个示例,但我无法弄清楚它正在尝试做什么。目前看来它似乎有语法错误。

相关问题