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

javascript function in jsp page - Stack Overflow

programmeradmin0浏览0评论

I have jsp page -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".dtd">
<html>
<head>
</head>
<body>

    <legend>Create new customer</legend>
    <script Language="JavaScript">
        function checkForm() {

            alert("YOU ARE IN checkForm FUNCTION !");
            return (false);
        }
    </script>
    <form action="CreateCustomerServlet" method="GET" onsubmit="checkForm">
        // form fields ... 
        <input type="submit" value="Create customer" />
    </form>
</body>
</html>

When I run on server this page and press on the submit button I see that it ignore checkForm and don't enter him , and directly go to CreateCustomerServlet .

My goals is that when press on submit it go to checkForm and if checkForm returns true only then it will go to CreateCustomerServlet . what I have to change in order to get this goal ?

I have jsp page -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>

    <legend>Create new customer</legend>
    <script Language="JavaScript">
        function checkForm() {

            alert("YOU ARE IN checkForm FUNCTION !");
            return (false);
        }
    </script>
    <form action="CreateCustomerServlet" method="GET" onsubmit="checkForm">
        // form fields ... 
        <input type="submit" value="Create customer" />
    </form>
</body>
</html>

When I run on server this page and press on the submit button I see that it ignore checkForm and don't enter him , and directly go to CreateCustomerServlet .

My goals is that when press on submit it go to checkForm and if checkForm returns true only then it will go to CreateCustomerServlet . what I have to change in order to get this goal ?

Share Improve this question asked Aug 3, 2012 at 9:08 URL87URL87 11k36 gold badges111 silver badges177 bronze badges 1
  • I wanted to know.. I have written same code in jsp but in wele.jsp... but it's not working there, can you please make me know why? – Pallav Raj Commented Jan 14, 2015 at 17:49
Add a ment  | 

3 Answers 3

Reset to default 3

Try

onsubmit="return checkForm();"

You should replace

onsubmit="checkForm"

By

onsubmit="return checkForm();"

You should use

onsubmit="return checkForm();"

because only when false is returned from onSubmit will it be stopped , or else it will continue submitting the form.

发布评论