如何将值从Jquery传递到jsp?

发布于 2024-10-04 20:36:56 字数 1310 浏览 2 评论 0原文

我的 jQuery 代码:

function check_availability(){   

         //get the username   
         alert("hai");
         var result ="";
         var CompanyCode = $('#CompanyCode').val();   
     alert("Jquery " +CompanyCode);
     //use ajax to run the check   
         $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   
             function(result){   
                 //if the result is 1   
                 alert("result"+result);
                 if(result == 1){   
                     //show that the username is available   
                     $('#username_availability_result').html(CompanyCode + ' is Available');   
                 }else{   
                     //show that the username is NOT available   
                     $('#username_availability_result').html(CompanyCode + ' is not Available');   
                 }   
         });   

}

checkUserName.jsp:

String companyCode = request.getParameter("CompanyCode");
out.println("companyCode"+companyCode);

rs = dbAccess.executeQuery("select companycode from yosemitecompany where companycode = '"+companyCode+"'");

如何从 jQuery 获取值?我需要在JSP页面中获取答案来检查该记录是否存在于表中。我没有从 request.getParamater() 方法 获取值。还有其他方法可以检查吗?

My jQuery code:

function check_availability(){   

         //get the username   
         alert("hai");
         var result ="";
         var CompanyCode = $('#CompanyCode').val();   
     alert("Jquery " +CompanyCode);
     //use ajax to run the check   
         $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   
             function(result){   
                 //if the result is 1   
                 alert("result"+result);
                 if(result == 1){   
                     //show that the username is available   
                     $('#username_availability_result').html(CompanyCode + ' is Available');   
                 }else{   
                     //show that the username is NOT available   
                     $('#username_availability_result').html(CompanyCode + ' is not Available');   
                 }   
         });   

}

checkUserName.jsp:

String companyCode = request.getParameter("CompanyCode");
out.println("companyCode"+companyCode);

rs = dbAccess.executeQuery("select companycode from yosemitecompany where companycode = '"+companyCode+"'");

How to get the values from jQuery? I need to get the answer in the JSP page for checking ,whether the record is present or not in the table. I am not getting the value from the request.getParamater() method. Is this any other way to check that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水中月 2024-10-11 20:36:56

而不是

     $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   

尝试

     $.post("checkUserName.jsp", { Code: CompanyCode },   

也许这是一个将键命名为与该变量相同的问题......

Instead of

     $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   

Try

     $.post("checkUserName.jsp", { Code: CompanyCode },   

Perhaps it's an issue with naming the keys the same as that var...

忘羡 2024-10-11 20:36:56

您的代码绝对有效。只有上面所说的变量命名可能是问题。
您可以尝试 jquery api 中的其他选项

$.post("checkUserName.jsp", $('#CompanyCode').serialize(),..

这应该可以解决您的问题。因为查询字符串值应该被引用( '')这个序列化方法可以正确地解决这个问题。

Your code is absolutely valid. Only thing as said above the variable naming may the issue.
You can try the other option from jquery api,

$.post("checkUserName.jsp", $('#CompanyCode').serialize(),..

This should take care of your issue. Because query string values should be quoted ('') properly. This serialize method would take care of that.

很糊涂小朋友 2024-10-11 20:36:56

看来CompanyCode就是id属性。所以只需创建一个属性name="something"
然后使用 request.getParameter("something")。我希望它能起作用

It seems that CompanyCode is the id attribute.So just create an attribute name="something"
then use request.getParameter("something").I hope it will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文