我正在写一个页面,将打印会员的徽章标签。这是一个DYMO标签制作器,我使用的是他们的javascript标签框架。
所有成员都与复选框一起列出,并在值中包含要为徽章编码的数据。用户将选中要为其打印徽章的成员,然后单击打印。
脚本将获取选中的复选框,并通过get请求将值传递给将返回编码数据的单独php文件。
要返回的数据是以句点分隔的,我需要将其拆分并将每个片段放在单独的行上。问题是我似乎无法获得超出get作用域的数据。
脚本的相关部分:
printButton.onclick = function() {
    try {
        printButton.disabled = true;
        settings.currentPrinterName = printersComboBox.value;
        var printer = printers[settings.currentPrinterName];
        if (!printer)
            throw new Error("Select printer");
        var label = null;
        if (printer.printerType == "LabelWriterPrinter") {
            label = addressLabel;
        }
        if (!label)
            throw new Error("Label is not loaded. Wait until is loaded or reload the page");
        var labelSet = new dymo.label.framework.LabelSetBuilder();
        var barcode
        $("#memchk :checked").each(function(){
            var value = $(this).val();
            var barcode;
            var record = labelSet.addRecord();
            $.get("http://ranch/sunrise/wolf/plugins/member_directory/views/barcode.php",{encString: value}, function(data){
                //alert(data.split("."));
                barcode = data.split(".");
            });
            alert(barcode[0]);
            record.setText("TEXT", barcode[0]);
            record.setText("TEXT_1", barcode[1]);
            record.setText("TEXT_2", barcode[2]);
            record.setText("TEXT_3", barcode[3]);
            record.setText("TEXT_4", barcode[4]);
            record.setText("TEXT_5", barcode[5]);
            record.setText("TEXT_6", barcode[6]);
            record.setText("TEXT_7", barcode[7]);
            record.setText("TEXT_8", barcode[8]);
            var memName = value.split("^");
            record.setText("TEXT_9", memName[0]);
        });
        //label.print(printer.name, null, labelSet.toString());
        saveSettings();
    } catch(e) {
        printButton.disabled = false;
        alert(e);
    }
    printButton.enabled = true;
}我尝试为get中的每一行设置文本,但它不起作用。get中的警报显示预期的数据,但如果我尝试按此方式运行它,它会显示条形码未定义。如果我注释掉get并将标签的文本行设置为静态字符串,它就能正常工作。
为什么我拿不到数据?
发布于 2011-05-19 22:29:08
而不是get,试试这个:
var barcode =  $.ajax({
        url: "http://ranch/sunrise/wolf/plugins/member_directory/views/barcode.php",
        type: "GET",
        data: {encString: value},
        async: false
    }).responseText;您的请求将分配条形码,并且如下所示:
        alert(barcode[0]);
        record.setText("TEXT", barcode[0]);
        record.setText("TEXT_1", barcode[1]);
        record.setText("TEXT_2", barcode[2]);
        record.setText("TEXT_3", barcode[3]);
        record.setText("TEXT_4", barcode[4]);
        record.setText("TEXT_5", barcode[5]);
        record.setText("TEXT_6", barcode[6]);
        record.setText("TEXT_7", barcode[7]);
        record.setText("TEXT_8", barcode[8]);应该可以工作;)
发布于 2011-05-19 22:19:13
get只是发出AJAX get请求的一种更短的方式。AJAX是异步的(这就是A所代表的)。这意味着请求将被发送,响应将在稍后返回。如果您希望在接收到响应之后执行get,则需要将您在该回调函数之后所做的所有工作移到回调函数中。
发布于 2011-05-19 23:06:27
不确定您使用的是哪个版本的jQuery,但我将从这里的文档开始:
http://api.jquery.com/jQuery.get/
另外,这些不是我的幻灯片(感谢Dan Heberden),但这里有一个很好的回调示例:
http://danheberden.com/presentations/deferreds-putting-laziness-to-work/#4
因此,您需要确保在回调函数中调用该函数来处理数据(带有条形码的部分= data.split(".")...上面的幻灯片还展示了如何使用延迟对象以更清晰的方式链接多个回调。我建议您在此处阅读此文档和延迟对象文档:
http://api.jquery.com/category/deferred-object/
简而言之,您应该能够使用回调正确地解决您的问题,但要获得更具可读性的解决方案,请查看deferreds。
https://stackoverflow.com/questions/6060035
复制相似问题