【问题标题】:Disable Checkbox with javascript使用 javascript 禁用复选框
【发布时间】:2015-02-15 20:03:11
【问题描述】:

在我的 rails 应用程序中,我在 profile/_experience_fields.html.erb 中有这个表单

    <div class="form-profile-half-last experselect end-date">
      <%= f.label :experience_end, class: "profile_label" %><br />
      <%= f.date_select :experience_end, {start_year: 1945, discard_day: true, order: [ :day, :month, :year ]}, :html=> {class: 'date-select'} %>
    </div>

    <div class="form-checkbox">
      <%= f.check_box :experience_current,:class => 'is_current', id: "checkbox_id" %>
      Je travail içi
    </div>

<script>
$(document).ready(function(){
    $("input[class='is_current']").change(function() {
       if ($('#checkbox_id').is(":checked")) {
         $('.end-date select').attr('disabled', true);
         $('.end-date select').css("background-color", "#D9D8DD");
         $('.end-date select').css("color", "#D9D8DD");
         $('.end-date select').css("-moz-appearance", "none");  

       }
       else {
         $('.end-date select').attr('disabled', false);
         $('.end-date select').css("background-color", "white");
         $('.end-date select').css("color", "#333");   
       }
    }); 
}); 
</script>

还有这个在 profile/edit.html.erb 中

<%= link_to_add_fields "Add another Experience" f, :experiences %>

一切正常,除非我使用复选框时遇到这个问题

1-检查第一个体验的复选框时,禁用经验和日期,但所有其他经验和日期都是禁用两个

2- 只有第一个使用其他复选框的表单添加链接添加不起作用

3- 如果保存带有选中复选框的表单并尝试对其进行编辑,我发现即使选中了复选框,体验和日期也已启用而不是禁用

所以我想知道如何解决所有这些问题,感谢您的帮助

【问题讨论】:

  • 执行 "$('#checkbox_id').prop("checked")" 而不是 "$('#checkbox_id').is(":checked")"。并执行“$('.end-date select').prop('disabled', true);”而不是“$('.end-date select').attr('disabled', true);” .
  • 这并没有改变任何东西

标签: javascript jquery ruby-on-rails ruby-on-rails-4 nested-attributes


【解决方案1】:

禁用的元素表单不会在提交的 POST 中发送。

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2014-10-19
    相关资源
    最近更新 更多