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
Add a ment  | 

3 Answers 3

Reset to default 6

Try 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