是否有一种方法可以从encodeURIComponent中排除某些字符

is there a way to exclude certain chars from encodeURIComponent

本文关键字:encodeURIComponent 排除 字符 方法 一种 是否      更新时间:2023-09-26

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

我想排除"&"answers"=",这样我就可以做出这样的声明:

first=blah&second=等等…

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

某些代码:

 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 + "&";

有没有办法从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或类似物将不能正确地对#=或其他必要的码点进行编码。

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

编辑—我试着根据你的代码创建一个例子,但我不知道它想做什么。目前看来,它似乎有语法错误。

相关文章:
  • 没有找到相关文章