javascript
  • asp.net
  • json
  • 2016-09-22 56 views 0 likes 
    0

    我正在嘗試使用JSON開關盒來顯示正常消息。我已經寫代碼也代碼低於如何在JSON中使用開關盒

    <script type="text/javascript"> 
        var text = '{ "R000092201": "EIN and Name should be same","F229000501":"Your Form is Incomplete, Please add the Credit Vehicle Details (Contact Support for any further queries)","F229002201":"Your Form is Rejected, Please ensure you select either EFW or EFTPS Payment (Contact Support for any further queries)"}'; 
    
        var obj = JSON.parse(text); 
        var clients = '<%= clients%>'; 
        alert(clients); 
        switch (clients) { 
         case 'R0000-922-01': document.getElementById('lblrejectionmsg').innerHTML = obj.R000092201; 
    
         case 'F2290-005-01': document.getElementById('lblrejectionmsg').innerHTML = obj.F229000501; 
    
         case 'F2290-022-01': document.getElementById('lblrejectionmsg').innerHTML = obj.F229002201; 
        } 
    </script> 
    

    在上面的代碼基於後端數據,即<%=客戶%>對應情況下應執行的,但在我的代碼是不例如客戶值的任何總是顯示第三種情況的消息。我哪裏去錯了,請幫助我。

    回答

    0

    您在每種情況下都缺少break;

    <script type="text/javascript"> 
        var text = '{ "R000092201": "EIN and Name should be same","F229000501":"Your Form is Incomplete, Please add the Credit Vehicle Details (Contact Support for any further queries)","F229002201":"Your Form is Rejected, Please ensure you select either EFW or EFTPS Payment (Contact Support for any further queries)"}'; 
    
    var obj = JSON.parse(text); 
    var clients = '<%= clients%>'; 
    alert(clients); 
    switch (clients) { 
        case 'R0000-922-01': document.getElementById('lblrejectionmsg').innerHTML = obj.R000092201; 
        break; 
        case 'F2290-005-01': document.getElementById('lblrejectionmsg').innerHTML = obj.F229000501; 
        break; 
        case 'F2290-022-01': document.getElementById('lblrejectionmsg').innerHTML = obj.F229002201; 
        break; 
    } 
    

    如果你錯過了破

    ;總是在匹配的情況下執行所有的情況。所以補充休息;在每個案例塊之後。

    +0

    'break;'是可選的。閱讀更多這裏https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch – Pugazh

    +1

    雖然'break;'是可選的,根據問題提出的問題,'break'是需要打破執行 –

    +1

    根據程序語義,Pugazh break是可選的,但如果只想運行一個case block,則需要中斷。 – VahagnNikoghosian

    相關問題