doty58493 2012-09-14 15:19
浏览 110
已采纳

传递具有特殊字符的字符串

I am trying to pass strings with spaces and special characters, but its getting error and nothing is working. e.g.

 <img onclick='addKeyword("celebritynews&gossip");' src="images/plus.png" >

but the '&' inside string celebritynews&gossip is breaking the code.

I figured out that the problem is in addKeyword function, I'd used the following code for defining a variable named url,

function addKeyword(categoryId){
var url = "addKeyword.php?uid=" + user_id + "&categoryId=" + categoryId + "&keyword=" + $("#keyword_" + categoryId).attr("value");
}

and that $("#keyword_" + categoryId) is causing problem. Any way to solve that?

  • 写回答

3条回答 默认 最新

  • douerqu2319 2012-09-14 15:21
    关注

    Assuming you are passing the string on a URL, the parameter values need to be URL encoded.

    ...SUMMARY EDIT...

    The & is interpreted as HTML and must be HTML encoded, specifically &amp;

    HTML

    <img onclick='addKeyword("celebritynews&amp;gossip");' src="images/plus.png" />
    

    JavaScript

    Use encodeURIComponent(s).

    var url = "addKeyword.php?uid=" + encodeURIComponent(user_id) + "&categoryId=" + encodeURIComponent(categoryId) + "&keyword=" + encodeURIComponent($("#keyword_" + categoryId).attr("value")); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?