如何在jQuery中的函数调用中传递2个值
How to pass 2 values in function call in jQuery
嗨,我有jQuery函数,在其中我调用了servlet,并传递了2个值,但它显示NullPointerException
。我不知道为什么,但我认为我在其中提出论点是错误的。
我的代码:
function addProductById(pId,pMqty){
$.getJSON("addtocart?pid=" + pId + "&minqty="+ pMqty +"&rand=" + Math.floor((Math.random()*100)+1), function(json) {
alert(json.msg);
});
}
在这里,我将2个变量pId和pMqty传递给servlet addtocart,但在servlet中没有接收到。知道它为什么不去那里吗?
我还将此函数调用为JavaScript,点击按钮:
onclick='addProductById(" + this.id + ","+ this.minqty +");'
能帮忙吗?
Java脚本代码:
tableRow: function tableRow(product){
this.id = product.pId;
this.image = product.pImage;
this.name = product.pName;
this.price = product.pPrice;
this.feature = product.pFeature;
this.minqty = product.pMqty;
var image = "<div class='mainitemlist'><div class='mainitemlistimage'><a href='product?pid=prdID'><img src='product_images/prdIMAGE' height='125px' width='100px' style='border-radius:2px;'></a></div>";
var name = "<div class='mainitemlistname'><div align='center'><a href='product?pid=prdID' style='color: #9caeb9;text-decoration: none;'>prdNAME</a></div></div>";
var price = "<div class='mainitemlistprice'><div align='center'>prdPRICE</div></div>";
var features = "<div class='mainitemlistfeatures'><div align='center'> <!--prdFEATURE-->MOQ : prdMINQTY</div><div align='center'><button type='button' style='display: none;margin-top: 5px;' class='btn btn-primary' onclick='addProductById(" + this.id + ","+ this.minqty +");'>Add To Cart</button></div></div></div>";
this.generateHTML = function generateHTML(){
html = "";
html += image.replace("prdIMAGE", this.image).replace("prdID", this.id);
html += name.replace("prdNAME", this.name).replace("prdID", this.id);
html += price.replace("prdPRICE", this.price);
html += features.replace("prdFEATURE", this.feature).replace("prdMINQTY", this.minqty);;
return html;
};
}
为了使用JavaScript显示数据,这里container
是div,我只在jsp页面中调用了这个div
display: function display(){
var node = document.getElementById('container');
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
var html = "";
for(var i = 0; i < catalogue.product.length; i++){
var tableRow = new catalogue.tableRow(catalogue.product[i]);
html += tableRow.generateHTML();
}
document.getElementById('container').innerHTML = html;
}
您可以这样做:-
onclick='addProductById("' + this.id + '","' + this.minqty +'");'
onclick
属性中的单引号没有正确闭合,这就是为什么这些值不起作用的原因。
并测试如下:-
function addProductById(pId, pMqty) {
// You can test the values here
alert(pId + ', ' + pMqty);
// your code here..
}
相关文章:
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- AngularJS和promise值在调用本地函数时的效果-未定义
- 使用for循环从Javascript中的函数返回多个值
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- 具有多个参数的checkbox函数调用
- 为什么这两个JavaScript函数调用具有相同的“;这个“;价值
- 如何在另一个函数调用中设置全局数组值
- 从下拉列表中选择多个值并调用javaScript函数
- 在 onchange 上调用 2 个 JS 函数,并且都返回一些值
- 根据两个异步函数调用的结果执行
- 如何在jQuery中的函数调用中传递2个值
- RSVP.js - 数组上的多个异步函数调用
- 链接或分组多个 JavaScript 函数调用,其中操作顺序有时至关重要
- 两个异步函数调用填充同一个数组
- 如何在内联 onclick 事件中进行两个 javascript 函数调用
- 角度$scope首先设置值而不是服务,然后第二个函数调用一切正常
- JSHint要求对多个赋值进行赋值或函数调用
- 简化了两个变更函数调用jquery
- 在提交表单上调用函数并显示两个值的和