2013-08-19 19 views
15

tôi có mã này:Làm thế nào để hiển thị/ẩn một phần tử trên các hộp kiểm được chọn/bỏ chọn bằng cách sử dụng jQuery?

<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> 

Và tôi muốn để hiển thị/ẩn các "câu trả lời" fieldset (mặc định là ẩn) sau khi một nhấp chuột vào hộp kiểm trong fieldset "câu hỏi" Làm thế nào để làm điều đó. Tôi không thể thực hiện điều đó bằng cách sử dụng kỹ thuật cho một bộ từ điển kinh điển như:

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

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

    }); 
</script> 

Ai đó có thể giúp tôi cách thực hiện điều đó bằng hộp kiểm? Ngoài ra nếu có thể null (bỏ chọn) hộp kiểm khi nó bị ẩn.

+0

: hãy lưu ý rằng .live() không được dùng kể từ phiên bản 1.7 của jQuery, thay vào đó sử dụng .on() như được đưa ra trong câu trả lời của Arun – zamil

Trả lời

26

Đính kèm onchange sự kiện vào hộp kiểm:

<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> 
3

Hãy thử

$(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']() 
    }); 

}); 

Demo: Fiddle

Hoặc

$(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); 
     } 
    }); 

}); 
+0

Ví dụ của bạn quá phức tạp (trong điều kiện rất khó để hiểu cho người mới bắt đầu; sự kiện mặc dù nó có thể là một đoạn mã hiệu quả), bạn có thể bình luận nó không. Tôi cần phải chạy những thứ khác nhau ngoại trừ hiển thị và ẩn. Đây có phải là cách duy nhất để làm điều tôi muốn làm không? – dmn77

+0

@ dmn77 xem cập nhật –

1
$(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); 
     } 
    }); 
}); 
26

Hãy thử điều này

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

FIDDLE

+0

+1 để ẩn với jquery thay vì hiển thị: không có; trong css. Điều này có vẻ đúng, chỉ trong trường hợp một số người dùng có thể bị tắt js. – Diego

+0

@EswaraReddy, +1, đây là câu trả lời hay nhất mà tôi đã tìm thấy cho đến nay – lmiguelvargasf

+0

@lmiguelvargasf Cảm ơn. –

1

Hãy thử điều này

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

đơn giản nhất - và tôi đã thay đổi lớp hộp kiểm để ID cũng như:

$(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>

Các vấn đề liên quan