刚才的帖子好像没说清楚,在这里求教各位高手了。
我的jsp页面代码如下<script type="text/javascript">
function goStop(userID){
alert(userID);
}
</script>
<html>
<head>
<title>List</title>
</head>
<body>
<table align="center" border=1>
<%
// list实际是从数据库中取得的数组,数据库中的字段可能有单引号,双引号,反斜杠等特殊字符
String[] list = { "user'14", "user'110", "user119","user\"120" };
for (int i = 0; i < list.length; i++) {
out.println("<tr>");
out.println("<td align='center'>" + list[i] + "</td>");
out.println("<td align='center'><input type='button' onClick=goStop(\"" + list[i] + "\") value='Stop' /></td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>
生成的页面中显示这4个数据,每个后面有个Stop按钮
user'14
user'110
user119
user"120 由于实际生成的部分Html页面代码是这样的<tr>
<td align='center'>user'14</td>
<td align='center'><input type='button' onClick=goStop("user'14") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user'110</td>
<td align='center'><input type='button' onClick=goStop("user'110") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user119</td>
<td align='center'><input type='button' onClick=goStop("user119") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user"120</td>
<td align='center'><input type='button' onClick=goStop("user"120") value='Stop' /></td>
</tr>所以最后一个user"120不能正常传到goStop这个Function里面,其他3个是ok的请问各位这里有什么好的办法来写这个onClick事件的代码
有个牛人说是用encoding来先做转换,能不能请教下各位怎么做转换,或者用什么其他方法来实现
我的jsp页面代码如下<script type="text/javascript">
function goStop(userID){
alert(userID);
}
</script>
<html>
<head>
<title>List</title>
</head>
<body>
<table align="center" border=1>
<%
// list实际是从数据库中取得的数组,数据库中的字段可能有单引号,双引号,反斜杠等特殊字符
String[] list = { "user'14", "user'110", "user119","user\"120" };
for (int i = 0; i < list.length; i++) {
out.println("<tr>");
out.println("<td align='center'>" + list[i] + "</td>");
out.println("<td align='center'><input type='button' onClick=goStop(\"" + list[i] + "\") value='Stop' /></td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>
生成的页面中显示这4个数据,每个后面有个Stop按钮
user'14
user'110
user119
user"120 由于实际生成的部分Html页面代码是这样的<tr>
<td align='center'>user'14</td>
<td align='center'><input type='button' onClick=goStop("user'14") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user'110</td>
<td align='center'><input type='button' onClick=goStop("user'110") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user119</td>
<td align='center'><input type='button' onClick=goStop("user119") value='Stop' /></td>
</tr>
<tr>
<td align='center'>user"120</td>
<td align='center'><input type='button' onClick=goStop("user"120") value='Stop' /></td>
</tr>所以最后一个user"120不能正常传到goStop这个Function里面,其他3个是ok的请问各位这里有什么好的办法来写这个onClick事件的代码
有个牛人说是用encoding来先做转换,能不能请教下各位怎么做转换,或者用什么其他方法来实现
解决方案 »
- 使用jsp怎么进行更新操作
- 接口和实现类的命名规则问题
- HTML提交到structs2的路径
- 关于Servlet的Cookie问题
- jsp 中的 查询数据问题
- 怎么让下载页面在弹出“另存为”对话框后自动关闭?
- HashMap的问题:用户点击过的新闻,我都用HashMap记录下来,并存入session中,在前台如果点击过的则提示,代码应该如何实现?!
- 请问在weblogic7下如何自己创建web application目录(光是一个DefaultWebApp根本不够)?
- 资源并发访问的问题.
- 调试出错~~JAVABEAN凋用不出来
- hibernate mysql setFirstResult 方法问题...
- TOMCAT,SERVLET,在一个自己写的类A 里调用另一个已经编译过的类B,编译出错
2 加上 \"
<a href="#" onclick='alert("this is\"good")'>clock</a>
<html><head>
<title>EncodeUrl→DecodeUrl</title>
<script type="text/javascript">
function Encode()
{
var text = document.getElementById("MyTextArea").value;
var encode = encodeURI(text);
document.getElementById("EncodeTextArea").value = encode;
}
function Decode()
{
var text = document.getElementById("EncodeTextArea").value;
var encode = decodeURI(text);
document.getElementById("MyTextArea").value = encode;
}
</script>
</head><body>
<table>
<tr>
<td>文本→
<button onclick="Encode()">EncodeUrl</button>
</td>
<td>Encode后
<button onclick="Decode()">DecodeUrl</button>
</td>
</tr><tr>
<td>
<textarea id="MyTextArea" cols="40" rows="10"></textarea></td>
<td>
<textarea id="EncodeTextArea" cols="40" rows="10"></textarea></td>
</tr>
</table>
</body></html>
我现在取得的是类似this is"good这样没有\的String,这样生成的Html页面里也没有\,所以从jsp到生成html以后,就从
<input type='button' onClick=goStop(\"" + "this is\"good" + "\") value='Stop' />
变成<input type='button' onClick=goStop("thi is"good") value='Stop' />
还是错的。
手动判断String中"的位置然后insert一个\倒是可以考虑,不过特殊字符有",',\这些,要考虑的太多TO 2楼:
goStop('user"120')的话,其他几个单引号的user'110不就出错了?TO 3楼:
这个页面我试了,可以实现成功转换
encodeURI这个方法可以解决所有特殊字符的问题,不过这里有个不同,你用document.getElementById取得了可能包含特殊字符的String
但是用我的写法,onClick=Encode()的时候,怎么把类似user"110这样没有\的String用onClick=Encode(user"110)传过去呢?
=。=
1."(双引号) 一旦引号出现问题很严重
2.大于小于号干扰 HTML
3.\(反斜杠)和\n\b\r等特殊字符的问题!!(一般没有问题)
看你的代码得知的的'单引号并不能干扰HTML所以不用理会!这个我曾经多次遇到 页面不能正确显示.
问题很好解决:把所有可能出现的字符都用
split 给替换成转义符.
<script type="text/javascript">
function goStop(userID){
alert(userID);
}
</script>
<html>
<head>
<title>List</title>
</head>
<body>
<table align="center" border=1>
<%
// list实际是从数据库中取得的数组,数据库中的字段可能有单引号,双引号,反斜杠等特殊字符
String[] list = { "user'14", "user'110", "user119","user\"120" };
for (int i = 0; i < list.length; i++) {
list[i] = list[i].replaceAll("&", "&"); //如果你的数据里跟定没有<>可以不要
list[i] = list[i].replaceAll("<", "<"); //如果你的数据里跟定没有<>可以不要
list[i] = list[i].replaceAll(">", ">"); //如果你的数据里跟定没有<>可以不要
list[i] = list[i].replaceAll("\'", "\\\'");//去除 单引号
list[i] = list[i].replaceAll("\"", "\\\"");//去除 双引号
list[i] = list[i].replaceAll("\\", "\\\\");//去除 反斜杠 out.println("<tr>");
out.println("<td align='center'>" + list[i] + "</td>");
out.println("<td align='center'><input type='button' onClick=goStop(\"" + list[i] + "\") value='Stop' /></td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>
在JS 里面, \" 代表字符串里面的双引号,比如
var str = "this is \"GOOD\""; // === this is "GOOD"而为了达到这个效果,你必须在JAVA里面对右斜杠和双引号做出处理
String str = "this is \"GOOD\"";
=>
String str = "this is \\\"GOOD\\\"";这样输出这个字符串的时候,才会变成前面的\"的效果
你可以用替换的方法编码你的原始字符串,请参考
String str = "this is \"GOOD\"";
System.out.println(str+"\n"+str.replace("\"","\\\""));
1. encodeURI,decodeURI应该是一种方法
2. 解决的时候用的是
String[] list = { "user'14", "user'110", "user119","user\"120","user\\130" };
String s = list[i];
s = s.replace("\\", "\\\\");
s = s.replace("\"", "\\\"");
这样的做法,单引号不考虑,把\和双引号转义总之是自己对jsp生成的html页面的转义符没弄清楚感谢各位尤其是老竹子的帮忙