html - Disable Drop Down Options Once Chosen In Other Dropdown in a Form for Rails -


i'm trying create form has list of dropdowns each use same options; i'd each option selected once. how can achieve this?

here general code form like:

    <div id="form">     <%= form_for :character, url: characters_path |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 doesn't prevent options being selected multiple times.

how can alter code prevent selection of same option multiple times?

use javascript. attach change event handler of select elements in page. when activated, event sets lists default value, except 1 being changed.
or checks if other values same current 1 selecting , alerts user.
or can validate in backend. when submit form, check if there values repeating, , throw error user.

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

or second option:

$(function() {   $("#first_stat").on("change", function() {     if ($("#second_stat").val() === $(this).val()) {       $(this).val("");       alert("value same second stat!")     }     // go on   });   // attach event rest of them. }); 

not beautiful, effective.
third option, should custom validations in rails.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -