如何使用$.相互配合



我有一个带有id的复选框列表,我可以选中复选框并通过执行以下操作获得所有id:

var order_id = [];      //array to store order ids from the checkboxes
var x = 0;  //count
checkboxes.each(function(){ //loop through the selected checkbox and store ids in array
if ($(this).hasClass('active')) {
order_id[x] = $(this).data('order-id');
x++; //increment x
});//end each   

我想要做的是将这些ORDER ID号打印到我的dymo标签打印机,为了做到这一点,我需要使用dymo提供的XML文件。该文件位于服务器上,我可以使用$.get

获取该文件
$.get(url+"labels/dymo_19x51.xml", function(labelXml) {
//framework
var label = dymo.label.framework.openLabelXml(labelXml);
//Order ID is set here
label.setObjectText("TEXT", order_id[x]);
//Label is printed here
label.print(myDymoPrinter);
}, "TEXT");

我遇到的问题是,如果我选择5个复选框,将打印5个标签,但它们都是相同的,即将打印最后选择的标签5次。我不能让它迭代$内的order_id数组。得到的功能。我试过在脚本的不同点增加x,但我就是不能让它发生。

任何帮助都是感激的。

很多谢谢,卡尔

我最终这样做了。我使用AJAX并将async设置为false

var order_id = $(this).data('order-id');
var qty = $(this).data('qty');
var delivery_method = $(this).data('shipping');
var full_name = $(this).data('fullname');
var dymo_txt = full_name +" (" + order_id + ")"+"nQty: " + qty + "n" + delivery_method;
$.ajax({
type: 'GET',
url: ajax_object.plugin_admin_assets+"labels/dymo_19x51.label",
async: false,
beforeSend: function() {
},
success: function(response) {
try {
var label = dymo.label.framework.openLabelXml(response);
label.setObjectText("TEXT", dymo_txt);
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0) {
throw "No DYMO printers are installed. Install DYMO printers." + dymo_txt;
}
var printerName = "";
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
//check that we are printing to the correct label writer
//can also use printer.printerType to get the the first label writer or if you only have 1
if (printer.modelName == "DYMO LabelWriter 450") {
printerName = printer.name;
break;
}//end if
}//end for
if (printerName == "") {
throw "No LabelWriter printers found. Install LabelWriter printer";
}

label.print(printerName);

}//end try  
}//end success
}); //end ajax

相关内容

  • 没有找到相关文章

最新更新