在复选框选择上显示div

时间:2013-11-25 17:28:09

标签: javascript jquery html css

HTML:

<div class='row homes center'>
  <div class='span1'></div>

  <div class='row homes center'>
    <div class='span1'></div>

    <div class='hometype span2'>
      <div class='checked'></div>

      <label for='prairie'>
        <img src='img/prairie.png' >
      </label>

      <input id='prairie' name='hometypes' type='checkbox' value='Prairie'>
    </div>

    <div class='hometype span2'>
      <div class='checked'></div>

      <label for='traditional'>
        <img src='img/traditional.png'>
      </label>

      <input id='traditional' name='hometypes' type='checkbox' value='Traditional'>
    </div>

    <div class='hometype span2'>
      <div class='checked'></div>

      <label for='transitional'>
        <img src='img/transitional.png'>
      </label>

      <input id='transitional' name='hometypes' type='checkbox' value='Transitional'>
    </div>

    <div class='hometype span2'>
      <div class='checked'></div>

      <label for='bungalow'>
        <img src='img/bungalow.png'>
      </label>

      <input id='bungalow' name='hometypes' type='checkbox' value="Bungalow">
    </div>
  </div>

  <div class='fixed_button homes hidden'>
    <a class='button blue_button continue continue_type'>Continue &rarr;</a>
  </div>
</div>

jQuery的:

$('.choose_style input[type="checkbox"]').on('change', function () {
  if ($('input[name=homestyles]:checked').val()) {
    $('.homes').removeClass('hidden');
  }
});

选中任何复选框后,我都需要显示按钮。单击复选框时没有任何反应。我究竟做错了什么?我需要从按钮中删除.hidden

3 个答案:

答案 0 :(得分:0)

如果检查集合中的任何元素,则

.is(':checked')返回true:

$('.choose_style input[type="checkbox"]').on('change', function () {
    if ( $('input[name=homestyles]').is(':checked') ) {           
        $('.homes').removeClass('hidden');
    }
});

请注意,发布的标记没有.choose_stylehomestyles元素?

答案 1 :(得分:0)

尝试:

$('.hometype  input[type="checkbox"]').on('change', function (){
    if ($('input[name="hometypes"]').is(':checked')) {           
        $('.fixed_button.homes').removeClass('hidden');
    }
    else{
        $('.fixed_button.homes').addClass('hidden');
    }
});

Fiddle here.

答案 2 :(得分:0)

你的代码中有很多错误。我修复了其中一些以使其正常工作http://jsfiddle.net/aamir/jNU4B/

$('input[type="checkbox"]').on('change', function () {
    if ($('input[name=homestyles]:checked')) {
        $('.homes').removeClass('hidden');
    }
 });