使用jquery将值设置为属性

我想为属性添加值。 我找到了这个,但我的要求有点不同。 我想在函数中设置值onclick="add('id', 5)"

 onclick="cart.add('', here i want to set with jQuery);" 

还请查看我的代码

更强的解决方案:

data-productid设置为输入和测试按钮:

 var cart = { // JUST TO TEST add : function(productid, val){ alert(productid +' '+ val); } } $(function () { // DOM ready $('.qtyplus, .qtyminus').on('click',function(){ var $qty = $('.featured-shopping-qty'); var currVal = Math.abs( parseInt($qty.val(), 10) ); var isPlus = $(this).hasClass("qtyplus"); var calc = currVal + (isPlus? 1 : -1) $qty.val( (!isNaN(calc) && calc>=0) ? calc : 0); }); $(document).on("click", ".throwToBasket", function(){ var productid = $(this).attr("data-productid"); var val = parseInt( $("input[data-productid='"+ productid +"']").val() , 10); cart.add(productid, val); }); }); 
  
test