使用jquery将值设置为属性

时间:2015-12-16 12:11:56

标签: javascript php jquery

我想为属性添加值。我found这个,但我的要求有点不同。我想在函数var triggeredEvents = []; $scope.$on('first', function() { notifyEventTriggered('first'); }); $scope.$on('second', function() { notifyEventTriggered('second'); }); function notifyEventTriggered(event) { if (triggeredEvents.indexOf(event) == -1) { triggeredEvents.push(event); } if (triggeredEvents.length > 1) { execute(); triggeredEvents.length = 0; } } 中设置值。

onclick="add('id', 5)"

请同时查看我的code

1 个答案:

答案 0 :(得分:1)

更强大的解决方案:

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);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="qtybox">
  <input type="text" name="quantity" value="0" class="qty featured-shopping-qty" data-productid="aaaaa">
</div>
<div class="plusminus">
  <div><input type="button" value="+" class="qtyplus"></div>
  <div><input type="button" value="-" class="qtyminus"></div>
</div>
<a href="javascript:void(0);" class="throwToBasket" data-productid="aaaaa">test</a>