gpt4 book ai didi

javascript - 如果选中则禁用表单(多表单)

转载 作者:行者123 更新时间:2023-12-03 07:28:21 37 4
gpt4 key购买 nike

我有经验,用户可以在那里输入他们的工作经验。因此,该表单是多种表单,用户可以添加尽可能多的体验。

例如,用户有两种工作经历,表单的 HTML 将为:

<div class="row">
<%= f.input :start_work, as: :date, start_year: Date.today.year - 20, end_year: Date.today.year, ignore_day: true, order: [:month, :year] %><br>
<%= f.input :is_current, input_html: {class: 'current'} %><br>
<%= f.input :end_work, input_html: {class: 'end_work'} , as: :date, start_year: Date.today.year - 20, end_year: Date.today.year, ignore_day: true, order: [:month, :year] %>
</div>

<div class="row">
<%= f.input :start_work, as: :date, start_year: Date.today.year - 20, end_year: Date.today.year, ignore_day: true, order: [:month, :year] %><br>
<%= f.input :is_current, input_html: {class: 'current'} %><br>
<%= f.input :end_work, input_html: {class: 'end_work'} , as: :date, start_year: Date.today.year - 20, end_year: Date.today.year, ignore_day: true, order: [:month, :year] %>
</div>

因此有两个元素的类为 current 和 end_work。现在,我希望当用户检查 is_current 时, end_work 将被禁用。所以,我添加这些 JS 代码:

$(document).ready(function() {
$(".current").each(function() {
$(this).click(function() {
var isCurrent = $(this).prop("checked");
if (isCurrent == true) {
$(".end_work").prop("disabled", "disabled");
}
else {
$(".end_work").prop("disabled", false);
}
});
});
});

它应该像这样工作:如果我检查第一个 is_current,那么第一个 end_work 字段将被禁用,而不影响第二个 end_work。

但是,当我检查第一个 is_current 时,它会影响第二个 end_work。我无法将每个表单的类更改为不同的,因为表单是使用 cocoon gem(嵌套表单)生成的。解决办法是什么?

更新 1

这是我的 HTML 代码:

<div class="form-group boolean optional resume_experiences_is_current">
<div class="checkbox">
<input value="0" type="hidden" name="resume[experiences_attributes][1][is_current]">
<label class="boolean optional" for="resume_experiences_attributes_1_is_current">
<input class="boolean optional current" type="checkbox" value="1" checked="checked" name="resume[experiences_attributes][1][is_current]" id="resume_experiences_attributes_1_is_current">Is current
</label>
</div>
</div>

<div class="form-group date optional resume_experiences_end_work"><label class="date optional control-label" for="resume_experiences_attributes_1_end_work_2i">End work</label><div class="form-inline"><input type="hidden" id="resume_experiences_attributes_1_end_work_3i" name="resume[experiences_attributes][1][end_work(3i)]" value="1">
<select id="resume_experiences_attributes_1_end_work_2i" name="resume[experiences_attributes][1][end_work(2i)]" class="date optional end_work form-control">
<option value="1">January</option>
<option value="2">February</option>
<option value="3" selected="selected">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="resume_experiences_attributes_1_end_work_1i" name="resume[experiences_attributes][1][end_work(1i)]" class="date optional end_work form-control">
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016" selected="selected">2016</option>
</select>
</div></div>

最佳答案

使用.next(SELECTOR)仅选择带有相应复选框next元素

试试这个:

$('.current').on('change', function() {
$(this).next('.end_work').prop('disabled', this.checked);
})

编辑:

$('.current').on('change', function() {
$(this).closest('.form-group').next('.form-group').find('.end_work').prop('disabled', this.checked);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="form-group boolean optional resume_experiences_is_current">
<div class="checkbox">
<input value="0" type="hidden" name="resume[experiences_attributes][1][is_current]">
<label class="boolean optional" for="resume_experiences_attributes_1_is_current">
<input class="boolean optional current" type="checkbox" value="1" checked="checked" name="resume[experiences_attributes][1][is_current]" id="resume_experiences_attributes_1_is_current">Is current
</label>
</div>
</div>

<div class="form-group date optional resume_experiences_end_work">
<label class="date optional control-label" for="resume_experiences_attributes_1_end_work_2i">End work</label>
<div class="form-inline">
<input type="hidden" id="resume_experiences_attributes_1_end_work_3i" name="resume[experiences_attributes][1][end_work(3i)]" value="1">
<select id="resume_experiences_attributes_1_end_work_2i" name="resume[experiences_attributes][1][end_work(2i)]" class="date optional end_work form-control">
<option value="1">January</option>
<option value="2">February</option>
<option value="3" selected="selected">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="resume_experiences_attributes_1_end_work_1i" name="resume[experiences_attributes][1][end_work(1i)]" class="date optional end_work form-control">
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016" selected="selected">2016</option>
</select>
</div>
</div>

Fiddle here

关于javascript - 如果选中则禁用表单(多表单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908757/

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