2017-04-24 7 views
0

グローバル変数を宣言し、複数選択イベントから値を取得するために使用しています。しかし、なぜモーダルウィンドウにこのエラーが表示されるのか混乱しています。面白いことは、他のvars dept、アドレスが正常に動作しているということです。なぜ私はReferenceErrorを取得していますか:ボックスが定義されていないエラー

ファイヤーブートコンソールでは、値が正しく表示されているのがわかります。誰かが私の誤りを指摘できるなら、私は感謝します。ありがとう

varボックスを宣言するコード。

<script> 

     $(function() { 
      var boxes; 
      $('.switch-item').click(function (e) { 
       e.preventDefault(); 

       var src = $(this).data('src'); 
       var dst = $(this).data('dst'); 
       var sel = $('#' + src + ' option:selected').detach(); 
       $('#' + dst).append(sel); 
       $("#boxdest option:selected").prop("selected", false) 
       $('#srcBoxRslt').val(''); 
       $('#srcBox').val(''); 
       $('#counter').html('Total selected boxes for destruction: ' + $('#boxdest2 option').length); 
       $("#submit").prop("disabled", false); 

       boxes = $('#boxdest2').val(); 
       console.log(boxes); 

      }); // end click 

      $('form').submit(function() { 
       if ($('#boxdest2').children().length == 0) { 

        notif({ 
         type: "error", 
         msg: "<b>ERROR:<br /><br />You must enter some box(es) for destruction</b><p>Click anywhere to close</p>", 
         height: 99, 
         multiline: true, 
         position: "middle,center", 
         fade: true, 
         timeout: 3000 

        }); 


        return false; 
       } 
       $('#boxdest2 option').prop({ 
        selected: true 
       }); 

      }); 

     }); 
</script> 

エラーが

<script type="text/javascript"> 
$(function() { 
    $('#destroy').click(function (e) { 
     $.Zebra_Dialog(
      'Department: ' + depts + 
      '<br />' + 
      'Address: ' + address + 
      '<br />' + 
      'Boxes: ' + boxes <--- ERROR 
     ,{ 
      'type': 'confirmation', 
      'title': 'Destroy' 
     }); 
    }); 
}); 
</script> 
+4

'私はグローバルvariable'を宣言している。このコードである - あなたは間違って...'のvar boxes'が(最初の '$内で宣言されているところですfunction(){...}); 'スコープ –

+0

depts/addressはどこで定義されていますか? –

答えて

1
<script> 
     var boxes; // this is the global scope. 

     $(function() { 
      var boxes; // Should not be here. This not the global scope. This is the scope of the function (which is passed on document.ready event) 

     }); 
</script> 
+0

この変更を行うと、さらにデバッグしてコードを動作させることができます。 – qwertynik

+0

乾杯。大いに助けてください – user1532468

関連する問題