admin管理员组文章数量:1432446
In my rails application i have this two field in my form and i was trying to disable the end_date field when the check box is checked, but didn't succeed, So I'm wondering on how can i achieve this? This is my form
<%= f.date_select :end_date, start_year: 1945 %>
<%= f.check_box :is_current %>
In my rails application i have this two field in my form and i was trying to disable the end_date field when the check box is checked, but didn't succeed, So I'm wondering on how can i achieve this? This is my form
<%= f.date_select :end_date, start_year: 1945 %>
<%= f.check_box :is_current %>
Share
Improve this question
edited Feb 6, 2015 at 18:28
BradleyDotNET
61.4k10 gold badges105 silver badges124 bronze badges
asked Feb 2, 2015 at 23:33
LoenvpyLoenvpy
9191 gold badge10 silver badges30 bronze badges
3 Answers
Reset to default 6Try adding this to your javascript file and it should work fine.
$( document ).ready(function() {
$("#checkBox_id").click(function() {
var isDisabled = $(#checkBox_id).prop('checked')
if(isDisabled) {
$("#endDate_id").removeAttr("disabled");
} else {
$("#endDate_id").prop('disabled', true)
}
});
});
Simple JS
$(document).ready(function() {
return $("#dateCheck").click(function() {
return $("#endDate").prop("disabled", !this.checked);
});
});
And HTML
<body>
<input type="checkbox" id="dateCheck" unchecked/>
<input type="date" id="endDate" />
</body
This should work. When the checkbox is checked, each of the select
fields generated by the default date_select
in Rails will be disabled. If this still doesn't solve your problem, post your full form so we can have a better idea as to what's going on.
$(document).ready(function() {
$("[id*='is_current']").click(function() {
var isCurrent = $(this).prop("checked");
var endDateSelects = $("[id*='end_date']");
if (isCurrent == true) {
endDateSelects.prop("disabled", "disabled");
}
else {
endDateSelects.prop("disabled", false);
}
});
});
本文标签: javascriptRails Disable form field when checkbox is checkedStack Overflow
版权声明:本文标题:javascript - Rails: Disable form field when checkbox is checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745590265a2665153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论