【问题标题】:Disable Drop Down Options Once Chosen In Other Dropdown in a Form for Rails在 Rails 表单的其他下拉菜单中选择后禁用下拉选项
【发布时间】:2015-07-13 16:59:48
【问题描述】:

我正在尝试创建一个表单,其中包含一个下拉列表,每个下拉列表都使用相同的选项;但是我希望每个选项只能选择一次。我怎样才能做到这一点?

这是我想要的表单的通​​用代码:

    <div id="form">
    <%= form_for :character, url: characters_path do |f| %>
      <p>
        <%= f.label :first_stat, "First Stat: " %>
        <%= f.select :first_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>

      <p>
        <%= f.label :second_stat, "Second Stat: " %>
        <%= f.select :second_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>  

      <p>
        <%= f.label :third_stat, "Third Stat: " %>
        <%= f.select :third_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>  

      <p>
        <%= f.label :fourth_stat, "Fourth Stat: " %>
        <%= f.select :fourth_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>                  

      <p>
        <%= f.label :fifth_stat, "Fifth Stat: " %>
        <%= f.select :fifth_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>                  

      <p>
        <%= f.label :sixth_stat, "Sixth Stat: " %>
        <%= f.select :sixth_stat, options_for_select([" ","strength","dexterity","constitution","intelligence","wisdom","charisma"], disabled: " ", selected: " ") %>
      </p>                  
      <p>
        <%= f.submit %>
      </p>
    <% end %>
</div> <!-- END OF FORM -->

显然这不会阻止选项被多次选择。

如何更改此代码以防止多次选择相同的选项?

【问题讨论】:

    标签: html ruby-on-rails forms ruby-on-rails-4


    【解决方案1】:

    使用javascript。将change 事件处理程序附加到页面中的所有select 元素。激活后,此事件会将所有列表设置为默认值,但正在更改的列表除外。
    或者它会检查其他值是否与您选择的当前值相同并提醒用户。
    或者您可以在后端验证这一点。提交表单时,检查是否有重复的值,并向用户抛出错误。

    $(function() {
      $("#first_stat").on("change", function() {
        $("#second_stat").val("");
        $("#third_stat").val("");
        $("#fourth_stat").val("");
        $("#fifth_stat").val("");
        $("#sixth_stat").val("");
      });
      // Attach the event to the rest of them.
    });
    

    或者第二个选项:

    $(function() {
      $("#first_stat").on("change", function() {
        if ($("#second_stat").val() === $(this).val()) {
          $(this).val("");
          alert("Value is the same as Second Stat!")
        }
        // Go on
      });
      // Attach the event to the rest of them.
    });
    

    不漂亮,但有效。
    对于第三个选项,您应该查看 Rails 中的自定义验证。

    【讨论】:

    • 我不太确定如何将更改事件处理程序链接到表单。我创建了一个 .js 文件,其中提供了第二个代码(我将其扩展为涵盖所有统计信息),称为priority_change_handler.js。我还在f.select 行的末尾添加了:, {}, {:onchange =&gt; "prioritize_change_handler();"}
    • 您不需要将此代码添加到选择行的末尾。将第二个选项添加到 js 文件中。如果 jQuery 和您创建的 javascript 文件已正确加载,当您选择第二个 stat 然后将第一个 stat 更改为相同时,它应该会提醒您一些事情。
    • 我不确定您所说的“将更改事件处理程序附加到页面中的所有选择元素。”,@MurifoX。
    猜你喜欢
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多