Html 从输入字段值设置 var 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2087399/
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
set var value from input field value
提问by framara
I started short time ago with JSP, JSTL, HTML and JavaScript so here is my problem:
我不久前开始使用 JSP、JSTL、HTML 和 JavaScript,所以这是我的问题:
I need to set the value of a var the value of an input hidden. Other option is if it possible to compare using
我需要将 var 的值设置为隐藏输入的值。其他选择是如果可以比较使用
<c:if test="....">
the value of a variable that I sent with the request with the value of the hidden input.
我随请求发送的变量的值以及隐藏输入的值。
Thanks.
谢谢。
Update
更新
I've been trying but can't make it work.
我一直在努力,但无法让它发挥作用。
I have this field that contains the id of and object. I also have the list with the objects so what I have to do is find the object related to that ID.
我有这个包含对象的 id 的字段。我还有包含对象的列表,所以我要做的是找到与该 ID 相关的对象。
<input type="text" name="id1" />
but if I do this:
但如果我这样做:
<c:set var="dd" value="${param.id1}" />
<input type="text" value="${dd}" />
The input text is empty but the text related to id1 displays 850 (i.e. the value is dinamic)
输入文本为空但与id1相关的文本显示850(即值为dinamic)
Any suggestion why is not working?
任何建议为什么不起作用?
Update 2
更新 2
I need the "multipart/form-data" because in the form I need to upload a picture. I understand how to get the parameters from Java, but since I'm not using the server but the JSP pages, there's any way to do it? Just need to read that input element and save it in a variable.
我需要“multipart/form-data”,因为在表单中我需要上传图片。我了解如何从 Java 获取参数,但是由于我使用的不是服务器而是 JSP 页面,有什么办法可以做到吗?只需要读取该输入元素并将其保存在变量中。
采纳答案by BalusC
You can access request parameters by implicit ${param}
variable.
您可以通过隐式${param}
变量访问请求参数。
E.g. http://example.com/context/page.jsp?foo=bar
in combination with
例如http://example.com/context/page.jsp?foo=bar
结合
<c:if test="${param.foo == 'bar'}">
The foo's param value is bar!
</c:if>
<c:if test="${param.foo != 'bar'}">
The foo's param value is not bar, it is: ${param.foo}
</c:if>
would show the first condition.
将显示第一个条件。
If you actually want to retain some hidden input element in subsequent requests (which wasn't really made clear in your question), then all you basically need to do is:
如果您真的想在后续请求中保留一些隐藏的输入元素(这在您的问题中没有明确说明),那么您基本上需要做的就是:
<input type="hidden" name="foo" value="${param.foo}">
Update: as per your update: you need to give the input element a nameas well. Thus, e.g.
更新:根据您的更新:您还需要为输入元素命名。因此,例如
<input type="text" name="id1" value="${param.id1}" />
This way it's available by request.getParameter("id1")
and inherently also ${param.id1}
. Do you see it now?
通过这种方式,它request.getParameter("id1")
本身也可以使用${param.id1}
。你现在看到了吗?
Update 2: as per your comment here: certainly this is related to enctype="multipart/form-data"
. With this encoding, the request parameters aren't in the parameter map anymore, but instead in the request body, because of the mixup with binary data (file uploads). It's going to be a long story to explain it all, but basically you need to parse the request yourself. If you're on Servlet 2.5 or older, then the Apache Commons FileUploadis very helpful here. Read especially "User Guide" and "Frequently Asked Questions" over there to see code examples and to learn how to use it the right way (also in MSIE!). You can even decide to abstract the FileUpload away so that you can stick using HttpServletRequest#getParameter()
and ${param}
the usual way, also see this article.
更新 2:根据您在此处的评论:当然这与enctype="multipart/form-data"
. 使用这种编码,请求参数不再在参数映射中,而是在请求正文中,因为与二进制数据(文件上传)混淆。解释这一切将是一个很长的故事,但基本上你需要自己解析请求。如果您使用的是 Servlet 2.5 或更早版本,那么Apache Commons FileUpload在这里非常有用。特别阅读那里的“用户指南”和“常见问题”以查看代码示例并学习如何以正确的方式使用它(也在 MSIE 中!)。您甚至可以决定将 FileUpload 抽象出来,以便您可以坚持使用HttpServletRequest#getParameter()
和${param}
通常的方式,另请参阅这篇文章。
If you're already on Servlet 3.0, then you can just make use of HttpServletRequest#getParts()
. You can even abstract it away so that you can stick using HttpServletRequest#getParameter()
and ${param}
the usual way, also see this article.
如果您已经在使用 Servlet 3.0,那么您可以使用HttpServletRequest#getParts()
. 你甚至可以把它抽象出来,这样你就可以坚持使用HttpServletRequest#getParameter()
和${param}
通常的方式,另见这篇文章。
Update 3:Oh, you reallydon't want to use JSP to do all the processing. There it is not for. It's high time to learn Servlet
. Besides, when using a Filter
which puts all parameters from the request body back in the request parameter map (as described in the both articles), you also don't necessarily need a Servlet
after all.
更新3:哦,你真的不想用JSP来做所有的处理。那里不是为了。是时候学习了Servlet
。此外,当使用 aFilter
将请求正文中的所有参数放回请求参数映射中时(如两篇文章中所述),Servlet
毕竟您也不一定需要 a 。