简体   繁体   中英

JQuery keypress function not working

I have the following code which checks if a roll number is valid in the database selected by the postname variable. It was working in an earlier version where I hadn't introduced the second variable postname. But at the moment, this code is not working. What's the error here?

    $(document).ready(function() {        //function to check rollno is valid
    $('#roll').keyup(function(event) {  
                    var rolll=$('#roll').val();
                    var postname=$('#post').val();
                    $.get('CheckRollValidity',{roll:rolll},{post:postname},function(responseText) { 
                        $('#status1').text(responseText);         
                    });
                });
    });

Servlet

        roll = request.getParameter("roll");
        temp = request.getParameter("post");
        table1 = "dbo."+post;
        table2 = "dbo.user_candidates";
        try 
        {
            if (roll.length() < 10 || roll.length() > 10) {
                result = "Please enter your " + len + "-digits roll number.";
                count1 = 1;
            } 
            else if (!roll.matches("[0-9]*")) 
            {
                result = "Please enter digits only";
                count1 = 1;
            }
            if (count1 == 0) 
            {
                 //database work
                 result="OK";
            } 
            else 
            {
                  result = "Error";
            }
         }
         response.setContentType("text/plain");
         response.setCharacterEncoding("UTF-8");
         response.getWriter().write(result);

Also, can I do the same via ajax, alternatively? Here I don't want the code working on pressing a submit button. Rather the working is happening on keypress.

You can't send two objects, you'll have to use one object with two values otherwise the second object is seen as the argument which should have been the callback

$.get('CheckRollValidity',{roll:rolll, post:postname},function(responseText) { 
    $('#status1').text(responseText); 
});

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