如何使用jQuery在复选框选中/未选中状态下显示/隐藏元素?

ogq8wdun  于 12个月前  发布在  jQuery
关注(0)|答案(8)|浏览(150)

我有这个代码:

<fieldset class="question">
       <label for="coupon_question">Do you have a coupon?</label>
       <input class="coupon_question" type="checkbox" name="coupon_question" value="1" />
       <span class="item-text">Yes</span>
   </fieldset>

   <fieldset class="answer">
       <label for="coupon_field">Your coupon:</label>
       <input type="text" name="coupon_field" id="coupon_field"/>
   </fieldset>

字符串
我想显示/隐藏“答案”字段集(默认为隐藏)后,在字段集复选框点击“问题”如何做到这一点.我无法做到这一点使用的技术为一个经典的elemetn喜欢:

<script>
    $().ready(function(){

        $('.question').live('click',function() {
                 $('.answer').show(300);
            }
            ,
            function(){
                $('.answer').hide(200);
            }
        );

    });
</script>


有人能帮我如何使用复选框吗?如果可能的话,也可以空(取消选中)复选框时,它的隐藏。

ruoxqz4g

ruoxqz4g1#

试试这个

$(".answer").hide();
$(".coupon_question").click(function() {
    if($(this).is(":checked")) {
        $(".answer").show(300);
    } else {
        $(".answer").hide(200);
    }
});

字符串
FIDDLE

uwopmtnx

uwopmtnx2#

onchange事件附加到复选框:

<input class="coupon_question" type="checkbox" name="coupon_question" value="1" onchange="valueChanged()"/>

<script type="text/javascript">
    function valueChanged()
    {
        if($('.coupon_question').is(":checked"))   
            $(".answer").show();
        else
            $(".answer").hide();
    }
</script>

字符串

prdp8dxp

prdp8dxp3#

最简单的-我也将复选框类更改为ID:

$(function() {
  $("#coupon_question").on("click",function() {
    $(".answer").toggle(this.checked);
  });
});
.answer { display:none }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<fieldset class="question">
  <label for="coupon_question">Do you have a coupon?</label>
  <input id="coupon_question" type="checkbox" name="coupon_question" value="1" />
  <span class="item-text">Yes</span>
</fieldset>

<fieldset class="answer">
  <label for="coupon_field">Your coupon:</label>
  <input type="text" name="coupon_field" id="coupon_field" />
</fieldset>
izkcnapc

izkcnapc4#

尝试

$(document).ready(function(){
    //Register click events to all checkboxes inside question element
    $(document).on('click', '.question input:checkbox', function() {
        //Find the next answer element to the question and based on the checked status call either show or hide method
        $(this).closest('.question').next('.answer')[this.checked? 'show' : 'hide']()
    });

});

字符串
演示:Fiddle

$(document).ready(function(){
    //Register click events to all checkboxes inside question element
    $(document).on('click', '.question input:checkbox', function() {
        //Find the next answer element to the question and based on the checked status call either show or hide method
        var answer = $(this).closest('.question').next('.answer');

        if(this.checked){
            answer.show(300);
        } else {
            answer.hide(300);
        }
    });

});

x8diyxa7

x8diyxa75#

试试这个

<script>
    $().ready(function(){
        $('.coupon_question').live('click',function() 
        {
            if ($('.coupon_question').is(':checked')) {
                $(".answer").show();
            } else {
                $(".answer").hide();
            } 
        });
    });
</script>

字符串

jfgube3f

jfgube3f6#

$(document).ready(function() {
    $(document).on("click", ".question", function(e) {
       var checked = $(this).find("input:checkbox").is(":checked");
       if (checked) {
           $('.answer').show(300);
       } else {
           $('.answer').hide(300);
       }
    });
});

字符串

jc3wubiy

jc3wubiy7#

你可以通过css:has()CSS伪类来实现

.answer{
display:none;
}
.question:has(input[type="checkbox"][value="1"]:checked) ~ .answer {
  display:block
}

个字符

k2fxgqgv

k2fxgqgv8#

<label  onclick="chkBulk();">
    <div class="icheckbox_flat-green" style="position: relative;">
      <asp:CheckBox ID="chkBulkAssign" runat="server" class="flat" 
       Style="position: 
         absolute; opacity: 0;" />
      </div>
      Bulk Assign
     </label>


    function chkBulk() {
    if ($('[id$=chkBulkAssign]')[0].checked) {
    $('div .icheckbox_flat-green').addClass('checked');
    $("[id$=btneNoteBulkExcelUpload]").show();           
    }
   else {
   $('div .icheckbox_flat-green').removeClass('checked');
   $("[id$=btneNoteBulkExcelUpload]").hide();
   }

字符串

相关问题