ruby-on-rails – 如何使用simple_form禁用下拉列表中的特定值?
发布时间:2020-12-16 22:53:43 所属栏目:百科 来源:网络整理
导读:我在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
我在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但我失败了. 解决方法
选择框的Simpleform构建器使用每个选项的值来与disabled属性的值进行比较,因此您只需使用组织的id来禁用所需的选项:
= simple_form_for(@organization,id: "chosen-select-speciality"},disabled: @organizations.first.id 如果要为selectbox禁用多个选项,可以手动构建内联选项列表或使用帮助程序并将其用作输入属性: = f.input :parent,collection: @organizations.map{|o| [o.id,o.name,{disabled: o.id.in?([1,21,10])}]},id: "chosen-select-speciality"} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |