简体   繁体   English

如何检查按钮是否被点击?

[英]How to check whether a button is clicked?

  <form action ="PurchaseCheckOut" method="POST" > 

               <input type = submit name="checkout" class ="button" value="Check Out">
               <%   if(request.getParameter("checkout") != null)
               list1.clear(); %>
          </form>

I'm using a ArrayList for list1. 我正在使用ArrayList作为list1。 So, when I click on the check out button, it will clear all the things in list1. 因此,当我单击“签出”按钮时,它将清除list1中的所有内容。 How can I do that ? 我怎样才能做到这一点 ? This is how I do it but it doesn't seems to work. 这是我的操作方式,但似乎无效。

EDIT : Still not working 编辑:仍然无法正常工作

<form action ="PurchaseCheckOut" method="POST" > 
               <input type = submit name="checkout" class ="button" value="Check Out" onClick="clearList()">
          </form>
       <%!
                public void clearList()
                {
                    list1.removeAll(list1);
                }
            %>        

Try adding onclick function to input. 尝试添加onclick函数进行输入。

<input type = submit name="checkout" class ="button" value="Check Out" onClick="clearList()">

function clearList(){ 函数clearList(){

//add code for clearing the list //添加代码以清除列表

} }

You have confused something. 你有些困惑。 HTML and javascript are running at the client side and jsp (Servlet) is running on the server side. HTML和javascript在客户端运行,而jsp(Servlet)在服务器端运行。 Once you click the button you can do one of the following: 单击按钮后,您可以执行以下操作之一:

  • request a new page with deleted data on the list and 请求列表中已删除数据的新页面,以及
  • call a javascript function which is deleting your data from the list. 调用从列表中删除数据的JavaScript函数。

Of course, the most proper approach to follow is to call a javascript function. 当然,最合适的方法是调用javascript函数。 In your code, you are calling a java function (jsp/servlet code). 在您的代码中,您正在调用Java函数(jsp / servlet代码)。 It's not possible to work with this way, because jsp/servlet is a code running on the server-side. 用这种方法是不可能的,因为jsp / servlet是在服务器端运行的代码。

<html>    
<body>

<select id="lstData">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>       

<input type = submit name="checkout" class ="button" value="Check Out" onClick="clearList()">

<script>
function clearList()
{
   var x=document.getElementById("lstData");
   x.innerHTML=""; //i'm not very sure about this, but it may work.   
}
</script>    

</body>    
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM