繁体   English   中英

单击复选框,删除最后一个输入字段

[英]Remove one last input field on click of checkbox

单击复选框后,我要隐藏最后一个输入字段。 我曾尝试prevclosest ,但它不工作。 几乎没有其他一组同类输入字段,因此我只想针对最后一个输入字段。 因此,单击此复选框后,我想隐藏最后一个输入字段job_to_date[] 下面是我的代码。

 jQuery('input[name^="currently_work_here"]').change(function() { if (jQuery(this).is(":checked")) { jQuery(this).prev('.job_to_date').remove(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-sm-6"> <input type="text" name="job_from_date[]" value="" placeholder="From" class="job_from_date"> </div> <div class="col-sm-6"> <input type="text" name="job_to_date[]" value="" placeholder="To" class="job_to_date"> </div> <div class="col-sm-12"> <input type="checkbox" name="currently_work_here[]" class="resume_builder_input input_checkbox"> I currently work here </div> 

根据修改后的HTML,您需要使用.parent().prev().find('.job_to_date')定位要删除的元素:

 jQuery('input[name^="currently_work_here"]').change(function() { if(jQuery(this).is(":checked")) { jQuery(this).parent().prev().find('.job_to_date').remove(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-sm-6"> <input type="text" name="job_from_date[]" value="" placeholder="From" class="job_from_date"> </div> <div class="col-sm-6"> <input type="text" name="job_to_date[]" value="" placeholder="To" class="job_to_date"> </div> <div class="col-sm-12"> <input type="checkbox" name="currently_work_here[]" class="resume_builder_input input_checkbox"> I currently work here </div> 

.prev()不能根据您的结构工作。 你需要去了通过电平.parent()然后以前的分度.prev()终于找到了一个与删除.find('.job_to_date')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM