【问题标题】:how to stop auto submit when using barcode machine使用条码机时如何停止自动提交
【发布时间】:2019-07-23 08:12:45
【问题描述】:

当我使用条形码阅读器并扫描条形码时,它会在表格中显示数据,当我再次扫描它而不是自动提交表单时如何停止它

//i try this code but it doesn't work
    <script type="text/javascript">
    $("#Barcode").keypress(function(event){
      if (event.which == '10' || event.which == '13') {
          event.preventDefault();
         }
    });
    </script>

<script type="text/javascript">
 /// Add Order Click btn////
        $(document).on('click', '.addOrder', function (event) {
            $('#order_form')[0].reset();
            var user_id = $(this).attr('id');
            $("#user_id").val(user_id);
            $('.modal-title').html("<i class='fa fa-plus'></i> Create Order");
            $('#action').val('Create Order');
            $('#btn_action').val('AddOrder');
            $('#span_product_details').html('');
        });

        ////barcode////
        var i = 0;
        $(document).on('change', '#Barcode', function () {
            var barcode = $(this).val();

            if (barcode == "") {
                return false;
            }
            else {
                var btn_action = "addbarcode";
                $.ajax({
                    url: base_url + 'admin/RetailerController/BarcodeOrder',
                    method: "POST",
                    dataType: "json",
                    data: {barcode: barcode, btn_action: btn_action},
                    success: function (data) {
                        if (data != "") {
                            $("#action_barcode").attr('disabled', false);
                            $("#bartable").show();
                            $("#helooo").append(data);

                            $('table #fTable td#ptotal').each(function (index) {
                                var total = 0;
                                $('#helooo tr').each(function () {
                                    total += +$('td.ptotal', this).eq(index).text(); //+ will convert string to number
                                });
                                total = (total).toFixed(2);
                                $(this).text(total);
                            })
                            $('table #fTable td#gsttotal').each(function (index) {
                                var total = 0;
                                $('#helooo tr').each(function () {
                                    total += +$('td.gst', this).eq(index).text(); //+ will convert string to number

                                });
                                total = (total).toFixed(2);
                                $(this).text(total);
                            })
                            $('table #fTable td#gtotal').each(function (index) {
                                var total = 0;
                                $('#helooo tr').each(function () {
                                    total += +$('td.gtotal', this).eq(index).text(); //+ will convert string to number
                                });
                                total = (total).toFixed(2);
                                $(this).text(total);
                            })
                        }
                        else {
                            alert('No Record Found');
                        }
                        i++;
                        
                        $("#Barcode").val('');
                        $("#Barcode").focus();
                        $("#Barcode").keypress(function(event){
                            if (event.which == '10' || event.which == '13') {
                                event.preventDefault();
                                    }
                                });
                    }
                })
            }
        });

        $(document).on('submit', '#order_form', function (event) {
            $('#action_barcode').attr('disabled', true);
            var dataid = $("#user_id").val();
            event.preventDefault();
            var form_data = $(this).serialize();

            $.ajax({
                url: base_url + 'admin/RetailerController/SaveOrder',
                method: "POST",
                data: form_data,
                success: function (data) {
                    $('#order_form')[0].reset();
                    $('#orderModal').modal('hide');
                    $('#alert_action').fadeIn().html('<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>' + data + '</div>');
                    var urlpage = base_url + 'admin/invoice/' + $.trim(data);
                    location.reload();
                    window.open(urlpage);
                }
            });
        });
<script>
<!-----------------Order Modal----------------->
<div id="orderModal" class="modal fade">
    <div class="modal-dialog">
        <form method="post" id="order_form" >
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" tabindex="8" id="Barcodecrox"
                            data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-plus"></i> Create Order</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <div class="form-group">
                            <div class="input-group col-md-6 col-sm-6 col-xs-8">
                                <span class="input-group-btn">
                                  <button type="button" disabled="disabled" class="btn">Barcode</button>
                                </span>
                                <input type="text" name="BarCode" id="Barcode" tabindex="1" autofocus="" class="form-control" placeholder="Scan Product Barcode">
                            </div>
                        </div>
                        <div id="bartable" style="display: none;">
                            <table class="table table-bordered table-hover">
                                <thead style="background-color: lightgray">
                                <tr>
                                    <th class="col-md-5">Product Name</th>
                                    <th>Barcode</th>
                                    <th>Price</th>
                                    <th>GST</th>
                                    <th>Total</th>
                                    <th>Action</th>
                                </tr>
                                </thead>
                                <tbody id="helooo"></tbody>
                                <tfoot id="fTable">
                                <tr>
                                    <td colspan="2"> Total</td>
                                    <td id="ptotal"></td>
                                    <td id="gsttotal"></td>
                                    <td id="gtotal"></td>
                                    <td></td>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                        <span id="span_product_details"></span>
                    </div>
                    <div class="row">
                        <div class="col-md-8 col-sm-8 col-xs-8">
                            <label style="font-size: 18px;margin-left: 5px">Discount</label>
                        </div>
                        <div class="col-md-3 col-sm-3 col-xs-3">
                            <input type="text" name="Discount" class="form-control"/>
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <input type="hidden" name="user_id" id="user_id"/>
                    <input type="hidden" name="RDType" id="RDType" value="<?php if (isset($TitleName)) echo $TitleName ?>"/>
                    <input type="hidden" name="btn_action" id="btn_action"/>
                    <input type="submit" name="action" id="action_barcode" disabled="disabled" class="btn btn-info" value="Create Order"/>
                </div>
            </div>
        </form>
    </div>
</div>
    <!-- End Order Modal -->
<script type = "text/javascript">
    $(document).on('change', '#Barcode', function () {
    var barcode = $(this).val();

    if (barcode == "") {
        return false;
    }
    else {
        var btn_action = "addbarcode";
        $.ajax({
            url: base_url + 'admin/RetailerController/BarcodeOrder',
            method: "POST",
            dataType: "json",
            data: {barcode: barcode, btn_action: btn_action},
            success: function (data) {
                if (data != "") {
                    $("#action_barcode").attr('disabled', false);
                    $("#bartable").show();
                    $("#helooo").append(data);

                    $('table #fTable td#ptotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.ptotal', this).eq(index).text(); //+ will convert string to number
                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                    $('table #fTable td#gsttotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.gst', this).eq(index).text(); //+ will convert string to number

                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                    $('table #fTable td#gtotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.gtotal', this).eq(index).text(); //+ will convert string to number
                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                }
                else {
                    alert('No Record Found');
                }
                i++;

                $("#Barcode").val('');
                $("#Barcode").focus();
                $("#Barcode").keypress(function(event){
                    if (event.which == '10' || event.which == '13') {
                        event.preventDefault();
                            }
                        });
            }
        })
    }
    });
</script>

实际上的问题是当我手动输入条形码编号时 完美运行,但是当我使用条形码阅读器时,它第一次扫描 条形码编号和显示数据但下一次同时提交 当用户扫描条形码编号然后显示数据时自动我想要 在表格中,当他单击提交按钮时,然后提交表单如何 解决这个问题。

【问题讨论】:

  • 也许您应该将事件处理程序添加到 &lt;form&gt; 标记。不在&lt;input&gt; 标签中
  • 感谢重播,但我不知道如何使用。请建议我是 codeigniter 的新手
  • 我试过这个而不是输入标签,但不起作用
  • 您正在表单提交处理程序的 AJAX 成功处理程序中重新加载完整页面,您确定这不是导致这种情况的原因吗?
  • 对不起,我听不懂你在说什么

标签: php ajax codeigniter barcode


【解决方案1】:

将此代码放在函数之外。

$("#Barcode").keypress(function(event){
    if (event.which == '10' || event.which == '13') {
        event.preventDefault();
    }
});

试试这个代码作为你的 javascript 函数。

<script type = "text/javascript">
$(document).on('change', '#Barcode', function () {
    var barcode = $(this).val();

    if (barcode == "") {
        return false;
    }
    else {
        var btn_action = "addbarcode";
        $.ajax({
            url: base_url + 'admin/RetailerController/BarcodeOrder',
            method: "POST",
            dataType: "json",
            data: {barcode: barcode, btn_action: btn_action},
            success: function (data) {
                if (data != "") {
                    $("#action_barcode").attr('disabled', false);
                    $("#bartable").show();
                    $("#helooo").append(data);

                    $('table #fTable td#ptotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.ptotal', this).eq(index).text(); //+ will convert string to number
                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                    $('table #fTable td#gsttotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.gst', this).eq(index).text(); //+ will convert string to number

                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                    $('table #fTable td#gtotal').each(function (index) {
                        var total = 0;
                        $('#helooo tr').each(function () {
                            total += +$('td.gtotal', this).eq(index).text(); //+ will convert string to number
                        });
                        total = (total).toFixed(2);
                        $(this).text(total);
                    })
                }
                else {
                    alert('No Record Found');
                }
                i++;

                $("#Barcode").val('');
                $("#Barcode").focus();

            }
        })
    }
});
    $("#Barcode").keypress(function(event){
        if (event.which == '10' || event.which == '13') {
            event.preventDefault();
        }
    });
</script>

【讨论】:

  • 我试过了,还是不行。实际上我使用条形码机,它扫描条形码编号,但是当它是按标签时,它会在表格中显示输出。我希望条形码阅读器扫描代码,然后按 Tab 而不显示输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2015-03-08
相关资源
最近更新 更多