我有经验形式,用户可以在那里输入他们的工作经验。因此,表单是多表单,用户可以添加尽可能多的体验。
例如,用户有两种工作经验,表单的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(嵌套表单)生成的。解决方案是什么?
这是我的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>
答案 0 :(得分:1)
使用.next(SELECTOR)
仅选择具有相应next
checkbox
元素
试试这个:
$('.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>
答案 1 :(得分:0)
在点击事件后尝试jquery中的next
函数。
$(document).ready(function() {
$(".current").each(function() {
$(this).click(function() {
var isCurrent = $(this).prop("checked");
if (isCurrent == true) {
$(this).next(".end_work").prop("disabled", "disabled");
}
else {
$(this).next(".end_work").prop("disabled", false);
}
});
});
});
&#13;