Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

java - jquery not working in JSP page

i have a HTML code

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
$( document ).ready(function() {

    $("#username").keyup(function(){

    var username = $("#username").val().trim();

    if(username=="")
    {
         $("#usernameerrortext").html("Username should not be empty");
         return false;
    }
    else if(/[!@#$%\^&*(){}[]<>?/|-]/.test(username))
    {
        $("#usernameerrortext").html("Username should not contain special charatcers");
        return false;
    }
    else
    {
        $("#usernameerrortext").html("");
        return true;
    }
    });

    $("#password").keyup(function(){
    var password = $("#password").val().trim();

    if(password=="")
    {
         $("#passworderrortext").html("Password should not be empty");
         return false;
    }
    else
    {
        $("#passworderrortext").html("");
        return true;
    }
    });
});
</script>
<br/>Username:<input type="text" name="username" id="username"/>
<font color="red"><div id="usernameerrortext"></div></font>
<br>
Password:<input type="password" name="password" id="password"/>
<font color="red"><div id="passworderrortext"></div></font>

This is working perfectly as a html page. but when i pasted this code in a JSP file like the below , my jquery is not working,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HDB SBBS</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
$( document ).ready(function() {

    $("#username").keyup(function(){

    var username = $("#username").val().trim();

    if(username=="")
    {
         $("#usernameerrortext").html("Username should not be empty");
         return false;
    }
    else if(/[!@#$%\^&*(){}[]<>?/|-]/.test(username))
    {
        $("#usernameerrortext").html("Username should not contain special charatcers");
        return false;
    }
    else
    {
        $("#usernameerrortext").html("");
        return true;
    }
    });

    $("#password").keyup(function(){
    var password = $("#password").val().trim();

    if(password=="")
    {
         $("#passworderrortext").html("Password should not be empty");
         return false;
    }
    else
    {
        $("#passworderrortext").html("");
        return true;
    }
    });
});
</script>
</head>
<body>
        <center>
            <h2>Login</h2>
            <form name="loginForm" id="loginForm" action="LoginServlet" method="post">
            <br/>Username:<input type="text" name="username" id="username">
            <font color="red"><div id="usernameerrortext"></div></font>
            <br/>Password:<input type="password" name="password" id="password">
            <font color="red"><div id="passworderrortext"></div></font>
            <br/><input type="submit" value="Submit">
            </form>
        </center>
    </body>
</html>

help needed here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the updated Jquery code:

$(document).ready(function() {

$("#username").on('focusout',function(){

var username = $("#username").val().trim();

if(username==="")
{    
     $("#usernameerrortext").html("Username should not be empty");
     return false;
}
else if(/[!@#$%\^&*(){}[]<>?/|-]/.test(username))
{
    $("#usernameerrortext").html("Username should not contain special charatcers");
    return false;
}
else
{
    $("#usernameerrortext").html("");
    return true;
}
});

$("#password").on('focusout',function(){
var password = $("#password").val().trim();

if(password==="")
{
     $("#passworderrortext").html("Password should not be empty");
     return false;
}
else
{
    $("#passworderrortext").html("");
    return true;
}
});
});

I have created a demo fiddle for you here is the FIDDLE

You need to include the jquery lib in head tag of the page you want to run this script.

For that nothing much has to be done just add this line in the head tag <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...