如何将值从Jquery传递到jsp?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而不是
尝试
也许这是一个将键命名为与该变量相同的问题......
Instead of
Try
Perhaps it's an issue with naming the keys the same as that var...
您的代码绝对有效。只有上面所说的变量命名可能是问题。
您可以尝试 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.
看来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