在java函数中使用javascript变量
想要一个关于如何访问 java 范围内的 javascript 变量的解决方案。 在下面的代码中,<%=toppings[k]%> k 是一个 javascript 变量,我如何访问它,我收到一条消息,因为 k JVM 不知道
function value_transitAccountCounter(i){
alert('welcome -->'+i);
for(var k=0;k<5;k++){
if(i==k){
return '<%=toppings[k]%>';
}
}
}
wanted a solution on how to access the javascript variable inside the java scope.
In the below code, <%=toppings[k]%>
k is a javascript variable how can i access that, I am getting a message as k is not known to JVM
function value_transitAccountCounter(i){
alert('welcome -->'+i);
for(var k=0;k<5;k++){
if(i==k){
return '<%=toppings[k]%>';
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 Java 代码正在服务器上运行。您的 JavaScript 代码正在客户端上运行。所以你不能在服务器上使用
k
,它在页面完成、发送到浏览器并执行其中的 JavaScript 之前不存在。从您的代码来看,您似乎只需要访问存储在服务器端
toppings
数组中的字符串。您有两种选择:将整个
toppings
数组作为 JavaScript 数组输出到页面的脚本中。然后您就可以在客户端使用它。让您的
value_transitAccountCounter
函数进行 ajax 调用 到您的服务器端代码,询问toppings[i]
的值是多少。您需要更改value_transitAccountCounter
以接受用于返回值的回调,而不是实际返回值,因为 ajax 调用将是异步的(value_transitAccountCounter
将在ajax 调用完成,因此,在您有要返回的值之前)。 (可能进行同步 ajax 调用,但这是一个非常非常糟糕的主意,你最好假装它不可能。当调用时,它完全锁定了大多数浏览器的 UI执行,令人难以置信令用户恼火。)Your Java code is running on the server. Your JavaScript code is running on the client. So you can't use
k
on the server, it doesn't exist until the page is finished, sent to the browser, and the JavaScript therein executed.From your code, it looks like you just need to access the strings stored in the server-side
toppings
array. You have two choices:Output the entire
toppings
array to the page's script as a JavaScript array. Then you can just use it client-side.Have your
value_transitAccountCounter
function make an ajax call to your server-side code, asking what the value oftoppings[i]
is. You'll need to changevalue_transitAccountCounter
to accept a callback to use to return the value rather than actually returning it, because the ajax call will be asynchronous (value_transitAccountCounter
will return before the ajax call completes and thus, before you have the value to return). (It's possible to make synchronous ajax calls, but it's a very, very bad idea and you're better off pretending it isn't possible. It completely locks up the UI of most browsers while the call is executing, incredibly irritating for users.)您无法从 JSP 访问 JavaScript 变量。 JavaScript 在客户端运行。 JSP 运行在服务器端。你可以这样写:
You can't access a JavaScript variable from JSP. The JavaScript runs on the client side. The JSP runs on the server side. You could write something like this instead:
JSP 在服务器上运行。它输出一些内容。内容被发送到浏览器。然后浏览器执行代码。
您无法在客户端和服务器端代码之间直接进行通信。
由于 JSP 代码首先运行,因此很容易将数据从 Java 获取到 JavaScript(您只需动态生成 JavaScript),但是您想采用其他方式。
为了将数据从客户端 JavaScript 变量获取到服务器端 JSP 变量,您必须通过以下方式发出新的 HTTP 请求:
location.href
XMLHttpRequest
该请求需要包含您想要传递的数据。
The JSP runs on the server. It outputs some content. The content is sent to the browser. The browser then executes code.
You cannot directly communicate between client side and server side code.
Since the JSP code runs first, it is easy to get data from the Java to the JavaScript (you just generate the JavaScript dynamically), however you want to go the other way.
In order to get data from a client side JavaScript variable to a server side JSP variable you must issue a new HTTP request by:
location.href
XMLHttpRequest
The request needs to include the data you want to pass.
克劳德是对的:
你不能集成你的javascript来查看java变量,但你可以做相反的事情,一般来说,总有一种方法可以反转你的问题,以便让你的javascript代码读取java变量。想一想。
祝你好运
Crowder is right:
you cannot intergrate your javascript in order to view java variable, but you can do the opposite, generally speaking, there is always a way to invert your problem in order to let your javascript code read java variable. Think about it.
Good luck