javascript 如何将javascript值分配给jsp页面中的jsp变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5715343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:06:23  来源:igfitidea点击:

how to assign javascript value to jsp variable in jsp page

javascriptjsp

提问by Anil Kumar

I have a text field, I need to send these text value to the same page which is in jsp .

我有一个文本字段,我需要将这些文本值发送到 jsp 中的同一页面。

I just want to assign javascript value to jsp variable.

我只想将 javascript 值分配给 jsp 变量。

回答by CloudyMarble

You can't. JSP runs on the server, Javascript runs on the client, so when Javascript runs the JSP variable doesnt exist anymore.

你不能。JSP 运行在服务器端,Javascript 运行在客户端,所以当 Javascript 运行时 JSP 变量不再存在。

Consider using AJAX.

考虑使用AJAX

回答by BalusC

You could set a hidden field.

你可以设置一个隐藏字段。

Put this in your JSP form:

把它放在你的 JSP 表单中:

<input type="hidden" id="foo" name="foo" />

Execute this script whenever you want to fill the field:

每当您想填充该字段时,请执行此脚本:

document.getElementById("foo").value = "some value";

When you submit the form, it'll be available as follows in the servlet:

当您提交表单时,它将在 servlet 中如下可用:

String foo = request.getParameter("foo"); // "some value"
// ...

See also:

也可以看看: