2012-11-20 4 views
0

조건이 true 인 경우에만 지정된 매개 변수가있는 버튼을 생성해야합니다. 어떻게 확인합니까? 아래에서 볼 수 있듯이이 시점에서 조건을 확인하지 않고 버튼이 생성됩니다. 올바른 구문으로 나를 도와주세요.jquery + json은 조건 (true 또는 false)으로 인해 버튼을 생성합니다.

JSON 코드 :

{ 
    "Caption": "Module caption", 
    "IconsDirectory": "C://Images/", 
    "Buttons": [ 
     { 
     "Conditions": [ 
      { 
      "ConditionText": "2 == 1", 
      "ButtonText": "Text_11", 
      "Visible": true, 
      "Colors": { 
       "FontColor": "#FFFFFF", 
       "BGColor": "#669933" 
      }, 
      "BButtonText": { 
       "TText": "Textt_1" 
      }, 
      "Size": { 
       "Width": 200, 
       "Height": 50 
      }, 
      "Icon": { 
       "FileName": "Smile.png", 
       "Width": 16, 
       "Height": 16 
      }, 
      "Url": { 
       "UrlAddress": "http://www.google.com", 
       "OpenNewWindow": true 
      }, 
      "JavaScriptAction": { 
       "TText": "alert('ok1');" 
      }}, 
     { 
      "ConditionText": "2 == 2", 
      "ButtonText": "Text_22", 
      "Visible": true, 
      "Colors": { 
       "FontColor": "#0FFFFF", 
       "BGColor": "#FF9966" 
      }, 
      "BButtonText": { 
       "TText": "Textt_1" 
      }, 
      "Size": { 
       "Width": 200, 
       "Height": 50 
      }, 
      "Icon": { 
       "FileName": "Smile.png", 
       "Width": 16, 
       "Height": 16 
      }, 
      "Url": { 
       "UrlAddress": "http://www.google.com", 
       "OpenNewWindow": true 
      }, 
      "JavaScriptAction": { 
       "TText": "alert('ok2');" 
      }} 
     ]} 
    ] 
} 

HTML 코드 :

<!DOCTYPE html> 
<html> 
<head> 
<title>SMButtons</title> 
<script src="jquery/jquery-1.4.2.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $.getJSON('weekendtask.json', function(data) { 
    var res = "" 
    $.each(data.Buttons, function(key, button) { 
     $.each(button.Conditions, function(key, condition) { 
      res += "<li>" 
      res += '<input type="button"' + '" value="' + condition.BButtonText.TText 
      res += '" onclick="' + condition.JavaScriptAction.TText 

      //background color 
      res += '" style=" background-color:' + condition.Colors.BGColor 

      //font color 
      res += '; color:' + condition.Colors.FontColor 

      res += '"/>' 
      res += "<\/li>" 
     }) 
    }) 
    $(res).appendTo('#ulObj') 
}); 
}); 
</script> 
</head> 
<body> 
<div> 
<ul id='ulObj'> 
<li>1</li> 
<li>2</li> 
<li>3</li> 
</ul> 
</div> 
</body> 
</html> 
+0

어떤 상태? 'if' 문은 어디에 있어야합니까? – ManseUK

+0

정확히 무엇을 확인하고 무엇을 확인해야하는지 설명해야합니다. – charlietfl

답변

0

는이 작업을 수행하여 Visible 값을 확인할 수 있습니다

$.each(data.Buttons, function(key, button) { 
    $.each(button.Conditions, function(key, condition) { 
     // check the visible value 
     if (condition.Visible) { 
     res += "<li>" 
     res += '<input type="button"' + '" value="' + condition.BButtonText.TText 
     res += '" onclick="' + condition.JavaScriptAction.TText 

     //background color 
     res += '" style=" background-color:' + condition.Colors.BGColor  

     //font color 
     res += '; color:' + condition.Colors.FontColor 

     res += '"/>' 
     res += "<\/li>" 
     } 
    }) 
}) 
+0

@ManseUK .. OP가 ContitionText 속성을 확인하려고합니다. –

+0

ManseUK, 감사합니다. – Marchello

관련 문제