使用jQuery将值设置为属性 [英] Set value to attribute using jquery
本文介绍了使用jQuery将值设置为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为属性添加值.我找到,但是我的要求没有什么不同.我想在功能onclick="add('id', 5)"
中设置值.
I want to add value to attribute. I found this one, but my requirments little different. I want to set value in function onclick="add('id', 5)"
.
onclick="cart.add('<?php echo $product['product_id']; ?>', here i want to set with jQuery);"
也请查看我的代码
推荐答案
更强大的解决方案:
将data-productid
设置为输入和测试按钮:
Set the data-productid
to both the input and the test button:
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>
这篇关于使用jQuery将值设置为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文