【问题标题】:How to pass 2 values in function call in jQuery如何在jQuery中的函数调用中传递2个值
【发布时间】:2013-11-19 07:13:05
【问题描述】:

您好,我有 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;        
    };

}

使用Java Script显示数据,这里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;
}

【问题讨论】:

  • 将 url:addtocart?pid=" + pId + "&amp;minqty="+ pMqty +"&amp;rand=" + Math.floor((Math.random()*100)+1) 打印到控制台并检查它是否是格式正确的 URL,并检查该 url 是否在浏览器中有效
  • @codeMan - 先生的问题是 pMqty 的值没有传入函数。问题出在onclick='addProductById(" + this.id + ","+ this.minqty +");',它完全只适用于pId,但添加minqty 后出现问题,我认为这是传递值的语法错误。

标签: java javascript jquery servlets


【解决方案1】:

你可以这样做:-

onclick='addProductById("' + this.id + '","' + this.minqty +'");'

onclick 属性中的单引号没有正确关闭,这就是为什么这些值不会出现。

并像这样测试它:-

function addProductById(pId, pMqty) {

    // You can test the values here
    alert(pId + ', ' + pMqty);

    // your code here..
}

【讨论】:

  • @palash - 我试过alert(pId + ', ' + pMqty);,但在 pMqty 中显示未定义。我认为问题出在onclick='addProductById(" + this.id + ","+ this.minqty +");',当我只使用this.pId 时,它可以工作,但不能处理数量数据。
  • 能否请您在使用minqty 属性的地方发布html 标记!?!?
  • 我没有在html中使用过,我在JavaScript中使用过。我正在发布我的 Java Script 的一部分。
  • 好的,请检查tableRow函数内部alert( this.minqty+', ' + product.pMqty)的值并将其放在代码this.minqty = product.pMqty;这一行之后...
  • 警报在 javascript tablerow 函数中不起作用。我已经按照你的建议做了,但它不起作用。在var features = "&lt;div class='mainitemlistfeatures'&gt;&lt;div align='center'&gt; &lt;!--prdFEATURE--&gt;MOQ : prdMINQTY&lt;/div&gt;&lt;div align='center'&gt;&lt;button type='button' style='display: none;margin-top: 5px;' class='btn btn-primary' onclick='addProductById(" + this.id + ","+ this.minqty +");'&gt;Add To Cart&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"; 这一行prdMINQTY 显示数量的值。我只需要这个显示未定义的值。
猜你喜欢
  • 2021-05-19
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多