javascript - Rails: Disable form field when checkbox is checked - Stack Overflow

In my rails application i have this two field in my form and i was trying to disable the end_date field

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);
    }
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745590266a4634787.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信