如何在 Java 中启用禁用的 JSP 按钮?

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

How to enable a disabled JSP button in Java?

javahtmljspservletsbutton

提问by pmark019

I want to have something like this:

我想要这样的东西:

public void setButton(){
     document.getElementById('scan').disabled=false; 
}

scan is the ID of the button in the JSP.

scan 是 JSP 中按钮的 ID。

回答by Santosh

What you are dealing here is html and javascriptand not java. Java based systems at the server will generate the html/css/jsbased code (after executing the JSP) and send it to browser. For enable/disabling and disabling the buttons, use javascript.

你在这里处理的是html 和 javascript不是 java。服务器上基于 Java 的系统将生成基于 html/css/js的代码(在执行 JSP 之后)并将其发送到浏览器。要启用/禁用和禁用按钮,请使用 javascript。

Not sure what you use case is, but you can use following javascript codecode to enable/disable the buttons

不确定您的用例是什么,但您可以使用以下javascript代码来启用/禁用按钮

document.getElementById("scan").disabled = true;

This can be called on any event (like page load etc)..

这可以在任何事件上调用(如页面加载等)。

EDIT:In light of new requirement(Capture USB events), this may not be as straightforward as it seemed. I would suggest following approach.

编辑:根据新要求(捕获 USB 事件),这可能不像看起来那么简单。我建议采用以下方法。

  1. Write a signed Java Applet. This Applet will use some USB interfacing APIs (e.g jUSB) to listen to the USB plugin events.
  2. Then, from this Applet use Applet Javascript interactionto call the javascript function to enable the button (assuming that the button is disabled when the page loaded).
  1. 编写一个签名的 Java Applet。该 Applet 将使用一些 USB 接口 API(例如jUSB)来监听 USB 插件事件。
  2. 然后,从这个Applet 使用Applet Javascript 交互调用javascript 函数来启用该按钮(假设该按钮在页面加载时被禁用)。

So it works as follows

所以它的工作原理如下

  • When you hit the URL, browser loads the page and Applet (with Scan button disabled by default)
  • You plugin the USB device
  • Java code in the applet listens to this event
  • The listener calls the Javascript function in the page which enables the Scan button.
  • 当您点击 URL 时,浏览器会加载页面和小程序(默认情况下禁用扫描按钮)
  • 您插入 USB 设备
  • 小程序中的Java代码监听这个事件
  • 侦听器调用页面中的 Javascript 函数以启用“扫描”按钮。

回答by Phony Lou

maybe you can use this

也许你可以用这个

document.getElementById("scan").disabled = true;

or jquery

或 jquery

$("#scan").disable = true;

回答by Suresh Atta

All the HTML in JSP compiles on server side and comes to Client.

JSP 中的所有 HTML 都在服务器端编译并生成Client.

If you want to do something you need to make a request.

如果你想做某事,你需要提出请求。

You can do it directly with html in your jsp

你可以在你的jsp中直接用html来做

<input type="button" name=myButton   id="scan"  value="disable" disabled>

If javascript

如果javascript

document.getElementById("scan").disabled=true;  //not false