jquery跳转url传汉字参数

url为GET提交,最大长度为255,而且一般不能传递汉字参数。
但,有时候不得不将中文参数传递,可以采用前后转码的方式。

[size=large][color=green]第一步,在javascript中转码。[/color][/size]

function urlChinese(name){
var url="view.do?name="+name;
url=encodeURI(url);
window.location.href=url;
}

html页面:
<a herf="#" onclick="urlChinese('张三')" >查看</a>

[color=green][size=large]第二步,在action中接收,并再次转码。[/size][/color]

private String name;(省略set、get方法)

//将乱码转为中文,需要try异常
String nameCha = java.net.Encoder.encode("name","UTF-8");


[color=green][size=medium]补充<a>标签的妙用。[/size][/color]
[color=red]1.提交表单 [/color]
<a href="#" onclick="submitFor('jvForm')">查 看<a>
jvForm 是要提交的表单的id属性。

[color=red]2.返回历史记录[/color]
<a href="#" class="btn_a1 return"
onclick="javascript:history.back(1)">返 回</a>

这样,页面讲返回到刚才的页面,历史记录。
jQuery Mobile 中,您可以使用 JavaScript/jQuery传递中文参数并获取它们。您可以使用 `encodeURIComponent` 函数对中文参数进行编码,然后将其作为查询字符串参数传递给超链接。 以下是一个示例代码: ```html <!DOCTYPE html> <html> <head> <title>jQuery Mobile 中文参数传递示例</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> </head> <body> <div data-role="page" id="page1"> <div data-role="header"> <h1>首页</h1> </div> <div data-role="content"> <a href="#" id="myLink">点击这里</a> </div> </div> <script> $(document).on("pagecreate", "#page1", function() { $("#myLink").on("click", function(e) { e.preventDefault(); var chineseParam = "中文参数"; var encodedParam = encodeURIComponent(chineseParam); var url = "page2.html?param1=" + encodedParam; // 跳转到 page2.html window.location.href = url; }); }); </script> </body> </html> ``` 上述代码中,我们创建了一个简单的页面,其中包含一个超链接 `<a href="#" id="myLink">点击这里</a>`。当用户点击该超链接时,通过 JavaScript/jQuery 的事件处理函数,我们获取中文参数 "中文参数" 并使用 `encodeURIComponent` 函数对其进行编码。然后,我们将编码后的参数添加到 URL 中,并使用 `window.location.href` 将页面重定向到带有中文参数的目标页面(在此示例中为 `page2.html`)。 在目标页面 `page2.html` 中,您可以使用 JavaScript/jQuery 来获取传递的中文参数并进行相应的处理。例如: ```html <!DOCTYPE html> <html> <head> <title>目标页面</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function() { var url = new URL(window.location.href); var encodedParam = url.searchParams.get("param1"); var chineseParam = decodeURIComponent(encodedParam); alert("传递的中文参数是:" + chineseParam); }); </script> </head> <body> <h1>目标页面</h1> </body> </html> ``` 在上述代码中,我们使用 JavaScript 的 `URL` 对象来获取 URL 中的查询字符串参数,并使用 `decodeURIComponent` 函数对编码后的中文参数进行解码。然后,您可以根据需要对该参数进行进一步处理。在此示例中,我们使用 `alert` 函数显示传递的中文参数。 请注意,上述代码仅用于演示目的,并假定 `page2.html` 是存在的页面。您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值