简体   繁体   English

一旦以Rails的形式选择了其他下拉菜单,则禁用下拉菜单选项

[英]Disable Drop Down Options Once Chosen In Other Dropdown in a Form for Rails

I'm trying to create a form that has a list of dropdowns that each use the same options; 我正在尝试创建一个包含下拉列表的表单,每个下拉列表都使用相同的选项; however I'd like for each option to only be selected once. 但是我希望每个选项只能选择一次。 How can I achieve this? 我该如何实现?

Here is the general code for the form that I would like: 这是我想要的表单的一般代码:

    <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 -->

Obviously this doesn't prevent the options from being selected multiple times. 显然,这不会阻止多次选择选项。

How can I alter this code to prevent the selection of the same option multiple times? 如何更改此代码以防止多次选择同一选项?

Use javascript . 使用javascript Attach an change event handler to all of the select elements in the page. change事件处理程序附加到页面中的所有select元素。 When activated, this event sets all lists to the default value, except the one being changed. 激活后,此事件会将所有列表设置为默认值,但更改的列表除外。
Or it checks if the other values are the same as the current one you are selecting and alerts the user. 或者,它检查其他值是否与您选择的当前值相同,并警告用户。
Or you can validate this in the backend. 或者,您可以在后端进行验证。 When you submit the form, check if there are values repeating, and throw an error to the user. 提交表单时,请检查是否有重复的值,并向用户抛出错误。

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

Or the second option: 或第二种选择:

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

Not beautiful, but effective. 不漂亮,但有效。
For the third option, you should look to custom validations in Rails. 对于第三个选项,您应该在Rails中查看自定义验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM