How do I check whether a checkbox is checked in jQuery?

前端 未结 30 3526
花落未央
花落未央 2020-11-21 04:44

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is

30条回答
  •  一整个雨季
    2020-11-21 05:00

    Though you have proposed a JavaScript solution for your problem (displaying a textbox when a checkbox is checked), this problem could be solved just by css. With this approach, your form works for users who have disabled JavaScript.

    Assuming that you have the following HTML:

    
    
    
    

    You can use the following CSS to achieve the desired functionality:

     #show_textbox:not(:checked) + input[type=text] {display:none;}
    

    For other scenarios, you may think of appropriate CSS selectors.

    Here is a Fiddle to demonstrate this approach.

提交回复
热议问题