java 如何在jsp中获取id而不是值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14315709/
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-31 15:51:23  来源:igfitidea点击:

How to Get an id in jsp instead of value?

javahtmljspservlets

提问by Rodel Sarate

how to get button id from jsp to servlet instead of getting the button value

如何从jsp获取按钮id到servlet而不是获取按钮值

<input id="${section.id}" type="submit" name="submit" value="Edit">

how to get that id in servlet?

如何在servlet中获取该ID?

回答by cowls

You can't that id is for client side use only. You will need to set the nameor valueto match the id of the element.

你不能那个 id 仅供客户端使用。您需要设置nameorvalue以匹配元素的 id。

Alternatively as a workaround you could create a hidden input field that contains the id value by adding something like this to your JSP:

或者,作为一种解决方法,您可以通过向 JSP 添加如下内容来创建一个包含 id 值的隐藏输入字段:

<input type="hidden" name="submit_id" value="${section.id}" />

This will then be available in the servlet upon form submit under the submit_idparameter.

然后,这将在 servlet 中在submit_id参数下提交表单时可用。

String submitId = (String)request.getParameter("submit_id");

回答by Kurt Du Bois

The only way you would be able to do that is by intercepting the form submit using javascript and setting the id as an extra post/get parameter.

您能够做到这一点的唯一方法是使用 javascript 拦截表单提交并将 id 设置为额外的 post/get 参数。

回答by Jose De Gouveia

the only way is make a javascript function that change your button value for the id , but i dont know why you want to do that, you could use a hidden input to send the data in the form

唯一的方法是创建一个 javascript 函数来更改您的 id 按钮值,但我不知道您为什么要这样做,您可以使用隐藏的输入来发送表单中的数据

<input type="hidden" name="id" value="the_id_number" />

回答by Renjith

You cannot get any button id value to servlet. When a request from browser is submitted, all the input fields(input tag) will be transferred to server.The value of each input attribute can be accessed using the name of that field.All other fields like id, class etc are used for css and JavaScript functionalists.You should not design to pass the button id to server side.Think of other methods like hidden input fields

您无法向 servlet 获取任何按钮 id 值。当来自浏览器的请求被提交时,所有输入字段(输入标签)将被传输到服务器。可以使用该字段的名称访问每个输入属性的值。所有其他字段,如 id、class 等都用于 css和 JavaScript 功能主义者。你不应该设计将按钮 id 传递给服务器端。想想其他方法,比如隐藏的输入字段