在JavaScript中将JavaScript变量分配给Java变量 [英] Assign JavaScript variable to Java Variable in JSP
问题描述
Ello那里,
我正在尝试将javascript变量的值赋给java变量。但我不知道怎么做?比方说我有这个:
I'm trying to assign the value of a javascript variable to a java variable. But I don't have clue how to do this? Say for example I have this:
<html>
<head>
<script type="text/javascript">
function return variable(){
var a = "hello";
return a;
}
</script>
</head>
<body>
<%
//The java code
String b = //how do I get that javascript variable here?
%>
</body>
</html>
推荐答案
Java脚本在浏览器上播放,其中java代码是服务器端所以你不能简单地这样做。
Java script plays on browser where java code is server side thing so you can't simply do this.
你可以做的是通过表单提交,或使用URL参数或使用AJAX将计算的变量从javascript提交到服务器电话,然后你可以在服务器上使用
What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server
HTML
<input type="hidden" id="hiddenField"/>
确保此字段位于< form> $ c $下c>
Javascript
document.getElementById("hiddenField").value=yourCalculatedVariable;
在服务器上你会得到这个作为请求的一部分
on server you would get this as a part of request
这篇关于在JavaScript中将JavaScript变量分配给Java变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!