简体   繁体   中英

How to disable a button from 'if' condition in jsp

I have a method Processsots which I am trying to invoke from submit button, But when I diasble by OnClick element. it is not calling the method. Any one help me how to call the method after disabling the submit button as well

<table>
    <tr>
        <td width=150> &nbsp; </td>
        <td width="1999">
            <h1 ALIGN='Left'>Please click on the Button to process Sots feed file</h1>
            <form name="sotstestorderfoler" >
                <br>
                <input type="submit" name="username" value="Process Feed File" onclick="this.disabled='disabled';>
            </form>
        </td>
    </tr>
</table>

<%
    if ( request.getParameter("username") != null ) {
        Processsots();  
    }
%>

(Code tried with onsubmit )

<form name="sotstestorderfoler" method="post" onsubmit="foo()" >
  <script type="text/javascript"> 
    function foo() { 
      sotstestorderfoler.username.disabled=true; <%Processsots();%> return true; 
    } 
  </script> 
</form>

I would use the onsubmit of the form to disable your button, like these answers illustrate:

Realize that JSP code is server-side and JavaScript is client-side, so they can't directly interact with each other.


You are mixing JSP code and JavaScript. The JSP code is only executed on the server. When your browser requests your webpage, JSP code is executed in order to build your webpage. That means that it starts writing out things like <script type="text/javascript"> and function foo() into a stream to send back to the browser. When it reaches <%Processsots();%> , it doesn't simply write out that text, it calls your method Processsots() . It will then write out the return value of Processots() . This means that your method is already executed before users even see your page or try to push the submit button.

This is a mistake that many new JSP programmers make; your JSP code cannot be executed while your JavaScript is executing. The only way for JavaScript code to interact with JSP code is via HTTP requests, usually in the form of an AJAX call or a form submit.

Instead of trying to call a JSP method in the JavaScript, you must have the form submit to a Servlet that can then call Processsots() and return the result as an HTTP response.

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