我试着在两个不同的按钮上安装两个条形码扫描仪。问题是,当我按下一个或另一个按钮时,两个脚本都用于每个按钮。我要怎么把他们分开?
按钮1:

<a class="uib-graphic-button default-graphic-sizing default-image-sizing hover-graphic-button active-graphic-button default-graphic-button default-graphic-text widget uib_w_52 d-margins skenavimo_tekstas media-button-text-bottom" data-uib="media/graphic_button"
                data-ver="0" id="scan1" style="position: relative; display:block; float:center; width: 10%; padding-bottom: 10%;
width: 50%; margin: 0 auto; " onclick="scanNow();">

按钮2:
<a class="button widget uib_w_54 d-margins testas icon camera yellow" data-uib="app_framework/button" data-ver="1" id="scan2" onclick="scanNow2();">Scan</a>

脚本1:
 <script>
        function scanNow()
            {
                //this function launches the QR Code scanner.
                intel.xdk.device.scanBarcode();
            }
            document.addEventListener("intel.xdk.device.barcode.scan",function(evt){
                if (evt.success == true) {
                    //successful scan
                   alert("I'm number 1");
                }
                else
                {
                    //failed scan
                    alert("ERROR");
                }
            },false);
    </script>

脚本2:
<script>
    function scanNow2()
            {
                //this function launches the QR Code scanner.
                intel.xdk.device.scanBarcode();
            }
   document.addEventListener("intel.xdk.device.barcode.scan",function(ted){
                if (ted.success == true) {
                    //successful scan
                alert("I'm nubmer 2");
                }else{
               //failed scan
                    alert("ERROR");
                }
             },false);
    </script>

最佳答案

您在代码中注册了两个事件处理程序,因此当您调用intel.xdk.device.scanBarcode();时,这两个处理程序都将被触发您应该有一个事件处理程序,并在事件处理程序中处理以下两个情况:

<script>

var scan = 0;

function scanNow(){
    scan = 1;
    intel.xdk.device.scanBarcode();
}

function scanNow2(){
    scan = 2;
    intel.xdk.device.scanBarcode();
}

document.addEventListener("intel.xdk.device.barcode.scan",function(ted){
    if (ted.success == true) {
        if(scan == 1){
            scan = 0;
            alert("I'm number 1");
        } else if(scan == 2){
            scan = 0;
            alert("I'm number 2");
        }
    }else{
        alert("ERROR");
    }
},false);

</script>

07-27 13:55