如何在JavaScript中替换URL中的空格? [英] How to replace a space in a URL in JavaScript?

查看:45
本文介绍了如何在JavaScript中替换URL中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是用JavaScript编写的函数中的2条相关行:

These are the 2 relevant lines from a function written in JavaScript:

var v_depttime = document.getElementById("EDDepttime").value ;
url = url+"?select_bno="+v_busno+"&select_depttime="+v_depttime ;

它将 select_depttime 发送为 2010-01-24 14:30:00 我希望它是一个URL编码的字符串,例如 2010-01-24%2014:30:00

It sends the select_depttime as 2010-01-24 14:30:00 and I want it to be an URL encoded string like 2010-01-24%2014:30:00

如何用JavaScript完成?

How is it done in JavaScript?

推荐答案

使用encodeURIComponent:

Use encodeURIComponent:

url = url +
  "?select_bno=" + encodeURIComponent(v_busno) +
  "&select_depttime=" + encodeURIComponent(v_depttime);

这篇关于如何在JavaScript中替换URL中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆