最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to call a JavaScript function from a button onclick in JSP page - Stack Overflow

programmeradmin5浏览0评论
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@page import="java.util.Map" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Login page</title>
    </head>
    <body>
     <link rel="stylesheet" type="text/css" href="css/login.css"/>
    <link rel="stylesheet" href=".10.2/themes/smoothness/jquery-ui.css" />

    <form action="LoginS" method="post">  
       <button type="submit"  onclick="javascript:MyFunction();">Login</button>
      <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
      </div>

      <script type="text/javascript">
    MyFunction(){
                 alert("java Script");
    }
    </script>
     </form>
    </body>
    </html>

Above is my code that I'm trying to call JavaScript function from the submit button onclick() event but its showing nothing. Am I doing any wrong in code.

When I click on the button nothing happens. I want the string value in the text box to be printed on the console.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@page import="java.util.Map" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Login page</title>
    </head>
    <body>
     <link rel="stylesheet" type="text/css" href="css/login.css"/>
    <link rel="stylesheet" href="http://code.jquery./ui/1.10.2/themes/smoothness/jquery-ui.css" />

    <form action="LoginS" method="post">  
       <button type="submit"  onclick="javascript:MyFunction();">Login</button>
      <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
      </div>

      <script type="text/javascript">
    MyFunction(){
                 alert("java Script");
    }
    </script>
     </form>
    </body>
    </html>

Above is my code that I'm trying to call JavaScript function from the submit button onclick() event but its showing nothing. Am I doing any wrong in code.

When I click on the button nothing happens. I want the string value in the text box to be printed on the console.

Share Improve this question edited Dec 19, 2016 at 11:32 Panky031 asked Dec 19, 2016 at 11:25 Panky031Panky031 4531 gold badge5 silver badges14 bronze badges 4
  • 3 where is the part that calls the function? also the correct javascript function syntax is function MyFunction(){} – Kevin Kloet Commented Dec 19, 2016 at 11:29
  • Sorry i just miss ..now i have added please have a look on it – Panky031 Commented Dec 19, 2016 at 11:33
  • Its working now : I have just changed the script code to like this : – Panky031 Commented Dec 19, 2016 at 11:35
  • Its working now : I have just changed the script code to like this : <script type="text/javascript"> function MyFunction(){ alert("java Script"); } </script> – Panky031 Commented Dec 19, 2016 at 11:36
Add a ment  | 

1 Answer 1

Reset to default 3

I've made some changes to make your code working and print "java script" in the console since there isn't any text box in your code :) I've also added a textbox to your code and printed its value as a sample for you.

I changed the type of login button to "button" and defined MyFunction as a function.

I also made a fiddle for it to test.

<form action="LoginS" method="post">  
    <button type="button"  onclick="javascript:MyFunction()">Login</button>
    <input id="text-box"/>
    <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
    </div>

    <script type="text/javascript">
        MyFunction = function(){
            console.log("java Script");
            console.log(document.getElementById("text-box").value);
        }
    </script>
</form>
发布评论

评论列表(0)

  1. 暂无评论