gpt4 book ai didi

Javascript 在 href 更改时删除方括号

转载 作者:行者123 更新时间:2023-11-30 12:55:15 24 4
gpt4 key购买 nike

这很奇怪。我有一些构建此查询字符串的 Javascript 代码:

var str = '/attorneys/?locations[]=70&locations[]=71&attorneyname=abc';
// p.s. in the real code, it actually builds str; it's not just declared like you see here :-)

window.location.href = str;

但是,浏览器正在删除方括号并重定向到:

http://mysite.com/attorneys?locations=70&locations=71&attorneyname=abc

为什么浏览器/Javascript 移除了方括号?我怎样才能让它们坚持下去?

最佳答案

同意楼上的意见,需要对[]字符进行编码。

尝试

str = '/attorneys/?locations%5B%5D=70&locations%5B%5D=71&attorneyname=abc';

有关编码的更多信息,请访问 http://www.w3schools.com/tags/ref_urlencode.asp (包括一些常用字符代码列表)

关于Javascript 在 href 更改时删除方括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com