2013-05-21 79 views
1

我正在檢查複選框是否已選中,並在頁面上顯示一個元素(如果已選中)。如何檢查是否與更改監聽器一起檢查複選框

這是我的函數:

function checkedTechSolutions(){  
    Y.one('#techsol input[type=checkbox]').on('change', function (e) { 
     var target = e.currentTarget, 
     techSBox = Y.one('#techsolutions'); 
     if (target.get('checked')){ 
      techSBox.show(); 
     } else techSBox.hide(); 
    } 
} 

這是我的CSS:

#techsolutions {width: 380px; height: 100px; background-color:#cee4f2; display: none;} 
#techsolutions .box {text-align: center;} 

這是我的html:

<div id="techsolutions"> 

    <div class=box> 
    <label for="TypeOfTS">Tech Solutions: </label> 
    <select name="techSolutionsDrop"> 
     <option value="techServices">Choose your services </option> 
    </select> 
    </div> 

</div> 
+0

看看完整的解決方案,如果你發佈的鏈接代碼jsfiddle.net –

回答

1

一些注意事項:

  • 您的示例中缺少的#techsol複選框
  • 當時checkedTechSolutions曾經叫什麼名字?在使用YUI時,您不需要文檔就緒類型事件來將偵聽器附加到複選框。
  • 使用css類來更改這樣的視覺效果。它更容易理解和更清潔。

採取這種jsfiddle

+0

Woops對不起,我忘了加我的複選框的html代碼會更容易些。這是在div之前。 –

+0

謝謝。我還有另外一個問題。我怎樣才能得到複選框,當選中時顯示在藍色框中。我意識到,父類是不能隱藏子類的,所以我如何實現這一點? –

+0

移動隱藏類,並更新JS像這樣:http://jsfiddle.net/jgdx/AxUMD/2/ –