Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何查找提交时使用的按钮_Javascript_Jquery_Forms - Fatal编程技术网

Javascript 如何查找提交时使用的按钮

Javascript 如何查找提交时使用的按钮,javascript,jquery,forms,Javascript,Jquery,Forms,我试图区分提交时按下的按钮/链接。我正在生成一个要购买的产品,如下所示: 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

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

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);​

但是我如何区分“添加到购物车”和正在按下的“立即购买”按钮呢?

将单击处理程序添加到按钮中,您可以在按钮中向已单击的元素添加一些数据,例如:

$('.button').click(function(){
 $(this).data("clicked", true);
});
然后在提交事件中检查此数据:

$('.button').each(function(){
 if ($(this).data("clicked")) alert("it was me");
});

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

$('#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
    }
});

Dave的答案可能更好,因此忽略此问题如果您使用
而不是
标记,则按钮的“name”属性与参数列表一起提交,参数值取自“value”属性。Dave的解决方案将适用于您。我还可以建议使用脚本模板,而不是使用javascript生成原始HTML吗?我得到了以下结论:uncaughttypeerror:Object#has no method'is'Oops。更新了参考。Hmmm仍有问题。总是去别的地方。想弄明白。警报(event.target.id);if($target.is(“#添加到购物车按钮”){alert('Ook');}else if($target.is(“#查看产品按钮”){alert('Ook 123');}else{var product_id=$(this).children('input[name=product_id]')).val();createPurchaseView(product#id);jQT.goTo('#购买')}这始终为空:event.target.idOk,我明白了为什么这不起作用。在提交事件中,event.target是表单,而不是按钮。因此,如果您将“on”函数中的操作更改为“click”而不是“submit”,这将很好地工作。请参见此处的代码:
$('#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
    }
});