【问题标题】:How to disable specific value in dropdown select with simple_form?如何使用 simple_form 禁用下拉选择中的特定值?
【发布时间】:2016-02-03 12:26:22
【问题描述】:

我在我的 rails 应用程序中使用 simple_form。我想禁用下拉列表中的特定值。

这是部分代码

= simple_form_for(@organization,url: admin_organization_path) do |f| 
 = f.input :hospital_name, input_html: { class: "form-control"}
 = f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}  

我尝试使用:disabled => @organizations.first,但失败了。 有没有其他方法可以使用。请帮助我。谢谢。

【问题讨论】:

  • 您好,您使用的是chosen-rails吗?

标签: ruby-on-rails simple-form multi-select jquery-chosen slim-lang


【解决方案1】:

选择框的简单构建器使用每个选项的值与禁用属性的值进行比较,因此您只需简单地使用组织的 ID 来禁用所需的选项:

= simple_form_for(@organization,url: admin_organization_path) do |f| 
 = f.input :hospital_name, input_html: { class: "form-control"}
 = f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}, disabled: @organizations.first.id

如果您想禁用选择框的多个选项,您可以手动构建选项列表内联或使用帮助器并将其用作输入属性:

= f.input :parent,  collection: @organizations.map{|o| [o.id, o.name, {disabled: o.id.in?([1,21,10])}]}, input_html: { class: "form-control", id: "chosen-select-speciality"}

【讨论】:

  • 它有效,但我如何禁用多个组织。
【解决方案2】:

通过使用 JavaScript,您可以轻松地禁用下拉列表中的特定值:

$(/* option selector */).prop('disabled', true);

查看action

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    相关资源
    最近更新 更多