[英]How do i pass parameters to JavaScript function via out.print in jsp?
My JavaScript function is: 我的JavaScript函数是:
function f1(name){
alert("Hello "+name);
}
My JSP code is: 我的JSP代码是:
<% out.print("<button onclick='f1(Rahul)'>Click me</button>"); %>
You can use escape sequences (\\) to do such jobs! 您可以使用转义序列(\\)来完成这些工作! Try this: 尝试这个:
<% out.print("<button onclick='f1(\"Rahul\")'>Click me</button>"); %>
Since Rahul
is a plain String
you want/need to pass it as parameter, you should escape it with quotes (") by using backslash (\\) character: 由于Rahul
是一个纯String
您希望/需要将其作为参数传递,因此应使用反斜杠(\\)字符将其转义为引号(“):
<% out.print("<button onclick='f1(\"Rahul\")'>Click me</button>"); %>
^escape the quote
Still, since you're already in JSP, you don't need to write the HTML code inside scriptlet, so you can change this to: 不过,由于您已经在JSP中,因此无需在scriptlet中编写HTML代码,因此可以将其更改为:
<%
...whatever code is here, probably a loop (very strange, indeed)
%>
<button onclick='f1("Rahul")'>Click me</button>
<%
rest of your scriptlet code...
%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.