gpt4 book ai didi

javascript - 如何查找提交时使用了哪个按钮

转载 作者:行者123 更新时间:2023-11-29 17:25:06 25 4
gpt4 key购买 nike

我试图区分提交时按下了哪个按钮/链接。我正在生成要购买的产品,如下所示:

var product = JSON.parse(data);

var html = '';
html += '<div>'
html += '<form class="buy-product" >';
html += '<div class="product_title">Price</div>'
html += '<div class= "product_info_2" >' + product['product_price'] + '</div>'
html += '<div class= "product_info_2" >' + product['product_description'] + '</div>'
html += '<div class="product_title">Image</div>'
html += '<div class= "product_info_2" >Coming Soon</div>'
html += '<input type="hidden" name="product_id" value="' + product['product_id'] + '" id="product_id" >'
html += '<a href="#" class="button rightButton submit" id="add-to-cart-button" style="margin-right:100px;" >Add To Cart!</a>'
html += '<a href="#" class="button rightButton submit" id="view-product-button" >Buy It Now!</a>'
html += '</form>'
html += '</div>'

$('#product .toolbar h1').html(product['product_name']);
$('#view-product').html(html);​

我了解到产品是这样提交的:

$('#product').on('submit', '.buy-product', function() {
var product_id = $(this).children('input[name=product_id]').val();
createPurchaseView(product_id);
jQT.goTo('#purchase');
});​

但是我如何区分“添加到购物车”和“立即购买”?

最佳答案

将事件参数添加到您的回调并检查其目标属性。

$('#product').on('click', '.buy-product', function(event) {
var $target = $(event.target);
if($target.is("#add-to-cart-button") {
//Add to cart
} else if($target.is("#view-product-button") {
//Buy it now
}
});

jQuery event.target page

关于javascript - 如何查找提交时使用了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9470160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com