5

我有这两个复选框:

<label for="cell">3. Do you currently have a cell phone? (Y/N)</label>
<input type="checkbox" name="cell" name="cell" id="cell" data-on="Yes" data-off="No" />

<label for="blackBerry">4.If YES, is the cell phone a BlackBerry? (Y/N)</label>
<input type="checkbox" name="blackBerry" name="blackBerry" id= "blackBerry" data-on="Yes" data-off="No" />

我怎样才能让第二个问题显示在页面上,只有当第一个问题被选为是时?

提前致谢。

4

3 回答 3

6

You can do this using javascript

Javascript

​var elem = ​document.getElementById('cell');

​elem.addEventListener('click', function() {
      var divElem = document.getElementById('divPhone'); 
    if( this.checked){
        divElem.style.display = 'block'  ; 
    }
    else{
        divElem.style.display = 'none'  ;
    }
});

HTML

   <label for="cell">3. Do you currently have a cell phone? (Y/N)</label>
    <input type="checkbox" name="cell" name="cell"
           id="cell" data-on="Yes" data-off="No" />

    <div id="divPhone" class="hidden">
      <label for="blackBerry">4.If YES, is the cell phone a BlackBerry?
                                                          (Y/N)</label>
      <input type="checkbox" name="blackBerry" name="blackBerry" 
            id= "blackBerry" data-on="Yes" data-off="No" />
    </div>

​.hidden 
{
    display: none;
}​

Check Fiddle

​It is still more simple using jQuery..

​$('#cell').on('click', function() {
    if(this.checked){
       $('#divPhone').removeClass('hidden');
    }
    else{
       $('#divPhone').addClass('hidden');
    }        
});​

jQuery Fiddle

于 2012-11-08T22:57:48.730 回答
3

仅 CSS:

<label for="cell">3. Do you currently have a cell phone? (Y/N)</label>
<input type="checkbox" name="cell" id="cell" data-on="Yes" data-off="No" />
<div class="hide_if_no">
    <label for="blackBerry">4. If YES, is the cell phone a BlackBerry? (Y/N)</label>
    <input type="checkbox" name="blackBerry" id="blackBerry" data-on="Yes" data-off="No" />
</div>

CSS:

input[type=checkbox] ~ div.hide_if_no {display:none}
input[type=checkbox]:checked ~ div.hide_if_no {display:block}
于 2012-11-08T22:35:53.290 回答
0

使用 jQuery,

$('#cell').change(function() {
    var c = this.checked;
    if (c) {
        $("#blackBerry").show();
    } else {
        $("#blackBerry").hide();

    }
});​
于 2012-11-08T22:36:04.933 回答