how to use javascript in jsp

后端 未结 2 510
情话喂你
情话喂你 2020-12-09 00:13

I want to call a javascript function that returns a value and then put that value in an if statement. There are two radio buttons in the HTML and the javascript checks to se

相关标签:
2条回答
  • 2020-12-09 00:28
    • You can not call JavaScript function in if statement of JSP, because JSP is executed at the server side and JavaScript is executed at client side.

    • You have to trigger event when the one of the radio button is clicked, using onclick event you can call function corc().

    • Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?

    JSP code:

    .......
    ........
    //use <form> to submit values to servlet
    
     <input type="radio" name="radio1" onclick="handleClick(this.id);" id="customerId" />
     <input type="radio" name="radio1" onclick="handleClick(this.id);" id="companyId" />
    ......
    .......
    //use hidden field to assign table value i.e. "customer" or "company".
     <input type="hidden" name="tableValue" id="tableTextId" />  
    //</form> closing form tag
    

    onclick event I assigned handleClick function and passed this.id, parameter this.id is used to pass the id attribute of the clicked radio button.

    JavaScript code:

    <script type="text/javascript">
      function handleClick(clickedId)
      {
         if(clickedId == "customerId")
           document.getElementById('tableTextId').value = "customer";
         else
           document.getElementById('tableTextId').value = "company";
      }
    </script>
    
    • When you will submit form then in servlet you can get the value of hidden field.

    String tableName = request.getParameter("tableValue"); // pass the name of hidden field i.e. tableValue

    • You can further pass this tableName to query.

    Related links

    • How to transfer data from JSP to servlet?
    • forms in HTML
    0 讨论(0)
  • 2020-12-09 00:53
    <input type="button" class="btn btn-danger" value="Delete" d=""+rs.getString(1)+"/>
    <script type="text/javascript">$(document).ready(function(){
        $(":Delete").click(function(){
            alert("Do you want to Delete this token : "+this.id);
        });
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题