【问题标题】:fill input fields with json data then sent to the printer用 json 数据填充输入字段,然后发送到打印机
【发布时间】:2016-10-19 09:09:50
【问题描述】:

我想用来自 json 的数据填充输入字段,然后将其发送到打印机,然后再次填充 db 中最后一行 -1 的输入,然后发送到打印机,最后一行 -2 然后打印等等。我已经设法根据 printmultiple 值从数据库中返回最后 n 行插入的 json 数据

html

<div id="printableArea">
<div align="center">

<input type="text" name="barcode2"  id="barcode2" required readonly >
<input type="text" name="barcodecountry" id="barcodecountry"  >
<input type="text" name="barcodecountry" id="barcodecountry2"  >

<br>
 <div id="print">
</div>

<label id="idbar">id.:</label><input type="text" name="barcodesurname" id="barcodesurname"  required readonly>
<br>
<br>
<label id="pricebar">price:</label><input type="text" name="barcodename" id="barcodename"  required readonly>
<input type="text" name="barcodecountry" id="barcodecountry3">
</div> 
</div>

</div>

printablearea 是输入已填充的 div,我正在发送打印到打印机

在我的 Js 文件中

$(document).ready(function() {


    $('form').submit(function(event) {

        $('.form-group').removeClass('has-error'); // remove the error class
        $('.help-block').remove(); // remove the error text





        var formData = {
            'surname'       : $('input[name=surname]').val(),
            'name'      : $('input[name=name]').val(),

            'telephone' : $("#telephone").val(),
            'mail' : $("#mail").val(),
            'barcode' : $("#barcode").val(),
            'customid' : $("#customid").val(),
            'ticketprice' : $("#ticketprice").val(),
            'seat' : $("#seat").val(),
            'printmultiple' :$("#printmultiple").val()

        };

        $.ajax({
            type        : 'POST', 
            url         : 'process2.php',
            data        : formData,
            dataType    : 'json', from the server
            encode      : true
        })

            .done(function(data) {



                console.log(data); 

                if ( ! data.success) {
                        if (data.errors.surname) {
                        $('#surname-group').addClass('has-error');
                        $('#surname-group').append('<div class="help-block">' + data.errors.surname + '</div>');
                        }
                        else if (data.errors.name) {
                        $('#name-group').addClass('has-error'); 
                        $('#name-group').append('<div class="help-block">' + data.errors.name + '</div>'); 
                        }



                } else {

 $.each(data.added, function (i,it){

    $('#printableArea').append(data.added[i]);
 });

  for(i=0; i<data.added[13]; i++){
                     $('#barcodecountry2').val(data.added[1]); 
                    $('#barcodesurname').val(data.added[0]);             
                    $('#barcodename').val(data.added[4]); 
                     $('#barcode2').val(data.added[2]);
                     $("#print").barcode(
    document.getElementById("barcode2").value,
    "code128" 
    );  
                $('#printableArea').print();
                 }

$('form').append('<div class="alert alert-success">' + data.message + '</div>');
    }
            })


            .fail(function(data) {


                console.log(data);
            });


        event.preventDefault();
    });

});

我希望从 json 中返回的数据来填充输入,例如要填充 json 的前 5 个元素,然后是 print,然后是下一个 5,然后是 print。我根据 printmultiple 值添加了打印代码,但只打印最后一行 n 次

下面我添加返回的json数据:Object { 0: "7784", 1: "2016-10-18 23:16:01.000000", 2: "16221458721646", 3: "2016-10-19", 4: "8", 5: "normal", 6: null, 7: "admin", 8: "DASDSADAS", 9: "7884", 18 more… }

【问题讨论】:

  • 如果您显示data 变量及其结构的示例内容可能会有所帮助。不过出于兴趣,这是一种非常奇怪的打印方式。这是报告吗?通常人们对使用文本框等打印表单不感兴趣,他们更喜欢数据格式良好且可读。打印出来的表格一般不会那么好看。表单用于输入和捕获数据,而不是显示/打印数据。
  • 该表格用于打印带有来自 db 的数据的条形码,但如果有人想打印例如 5 个条形码,我希望在 db 上插入最后 5 行,因为条形码采用条形码 + 1 并打印在打印机上
  • json Object 返回的数据 { 0: "7784", 1: "2016-10-18 23:16:01.000000", 2: "16221458721646", 3: "2016-10 -19", 4: "8", 5: "normal", 6: null, 7: "admin", 8: "DASDSADAS", 9: "7884", 还有 18 个……}
  • 我知道表单是干什么用的,我在问你为什么使用表单元素作为媒介,而不是其他一些更容易格式化的html?表单元素用于输入,而不是输出。请编辑问题并包含格式正确的 JSON 示例 - 很难读取插入 cmets 的代码/数据:-)

标签: javascript jquery html json ajax


【解决方案1】:

我找到了解决办法

   for(i=0; i<data.added[0].printmultiple; i++){
                     $('#barcodecountry2').val(data.added[i].signintime); 
                    $('#barcodesurname').val(data.added[i].customid);             //get id 
                    $('#barcodename').val(data.added[i].ticketprice); 
                     $('#barcode2').val(data.added[i].barcode);
                     $("#print").barcode( //div id print
    document.getElementById("barcode2").value,// Value barcode (dependent on the type of barcode)
    "code128" // type (string)
    );  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多