简体   繁体   中英

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. So, when I click on the check out button, it will clear all the things in 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.

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

function 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. 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.

Of course, the most proper approach to follow is to call a javascript function. In your code, you are calling a java function (jsp/servlet code). It's not possible to work with this way, because jsp/servlet is a code running on the server-side.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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