簡體   English   中英

Rails:選中復選框時禁用表單字段

[英]Rails: Disable form field when checkbox is checked

在我的Rails應用程序中,我的表單中有這兩個字段,並且我試圖在選中復選框時禁用end_date字段,但未成功,所以我想知道如何實現此目的? 這是我的表格

<%= f.date_select :end_date, start_year: 1945 %> 
<%= f.check_box :is_current %>

嘗試將其添加到您的javascript文件中,它應該可以正常工作。

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

簡單的JS

$(document).ready(function() {
  return $("#dateCheck").click(function() {
    return $("#endDate").prop("disabled", !this.checked);
  });
});

和HTML

<body>
    <input type="checkbox" id="dateCheck" unchecked/>
    <input type="date" id="endDate" />
</body

這應該工作。 選中該復選框后,將禁用Rails中默認date_select生成的每個select字段。 如果仍然不能解決您的問題,請發布完整的表單,以便我們對發生的事情有一個更好的了解。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM