jquery - show textbox when checkbox checked

后端 未结 7 769
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 08:44

I have this form

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 09:42

    As your dividers are placed next to your checkboxes, you simply need to use jQuery's next() method to select the correct elements:

    if ($(this).is(':checked'))
        $(this).next('div.max_tickets').show();
    else
        $(this).next('div.max_tickets').hide();
    

    Updated Codepen demo.

    From the documentation (linked above), the next() method selects:

    ...the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

    Here we're selecting the next div.max_tickets element. However in your case just using next() with no parameters would suffice.

提交回复
热议问题