使用jQuery一键触发多个添加到篮子按钮

时间:2012-04-12 14:54:18

标签: jquery

我希望在现成的电子商务平台上触发多个添加到篮子按钮。服务器使用以下脚本生成添加到购物篮按钮:

按钮1:

<a id="productRepeater_ctl00_btnAddToBasket" class="btn addbutton" href="javascript:__doPostBack('productRepeater$ctl00$btnAddToBasket','')">add to basket</a>

按钮2:

<a id="productRepeater_ctl01_btnAddToBasket" class="btn addbutton" href="javascript:__doPostBack('productRepeater$ctl01$btnAddToBasket','')">add to basket</a>

按钮1:

<a id="productRepeater_ctl02_btnAddToBasket" class="btn addbutton" href="javascript:__doPostBack('productRepeater$ctl02$btnAddToBasket','')">add to basket</a>

平台脚本:

//<![CDATA[
var theForm = document.forms['buyoffpage'];
if (!theForm) {
    theForm = document.buyoffpage;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>

我希望能够通过单击一个单独的jQuery按钮提交所有这些按钮。我到目前为止的脚本只提交了第一个:

$("body").append('<div id=\"submitAll\"><a href=\"#\">HIT ME</a></div>');
$('#submitAll').click(function(e) {
   __doPostBack('productRepeater$ctl00$btnAddToBasket', 'productRepeater$ctl01$btnAddToBasket','productRepeater$ctl02$btnAddToBasket');
    }); 

2 个答案:

答案 0 :(得分:0)

如果表格未通过AJAX或类似方式提交,您将无法提交所有表格。如果你使用的是AJAX,你可以这样做:

var submitAll = $('<a href="javascript:;" />').html('HIT ME').click(function(){

      $('.addbutton').trigger('click');

});
$('body').append($('<div id="submitAll" />').append(submitAll));

答案 1 :(得分:0)

你会想做这样的事情......(你不得不玩这个,因为它不是一个完整的解决方案)

function submitAll(){
  //your submit code
}

$(".btn.addbutton").each(function(a){
  //first clean up old junk
  a=$(a)
  a.removeAttr('href');
  //add a custom event call
  a.bind('submitAll', submitAll )
});

//fire custom events
$('#submitAll').trigger('submitAll')