gpt4 book ai didi

javascript - jquery - 选中复选框时显示文本框

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:19 30 4
gpt4 key购买 nike

我有这个表格

<form action="">
<div id="opwp_woo_tickets">
<input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][0][enable]">
<div class="max_tickets">
<input type="text" name="opwp_wootickets[tickets][0][maxtickets]">
</div>

<input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][1][enable]">
<div class="max_tickets">
<input type="text" name="opwp_wootickets[tickets][1][maxtickets]">
</div>

<input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][2][enable]">
<div class="max_tickets">
<input type="text" name="opwp_wootickets[tickets][2][maxtickets]">
</div>
</div>
</form>

截至目前,我正在使用此 jquery 代码在选中复选框时显示文本框。

jQuery(document).ready(function($) {
$('input.maxtickets_enable_cb').change(function(){
if ($(this).is(':checked')) $('div.max_tickets').show();
else $('div.max_tickets').hide();
}).change();
});

它工作正常,但它会在选中时显示所有文本框。

有人可以帮我解决吗?

这是我的问题的演示。

http://codepen.io/mistergiri/pen/spBhD

最佳答案

由于分隔线位于复选框旁边,因此您只需使用 jQuery's next() method选择正确的元素:

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

Updated Codepen demo .

根据文档(上面的链接),next() 方法选择:

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

这里我们选择下一个 div.max_tickets 元素。但是,在您的情况下,仅使用不带参数的 next() 就足够了。

关于javascript - jquery - 选中复选框时显示文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670323/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com