Created
October 18, 2013 15:01
-
-
Save JonMidhir/7042874 to your computer and use it in GitHub Desktop.
Here's a simple way to make multiple select fields dependent on one another. When a value is selected in one it is disabled in the others and cannot be selected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.dependentSelect = -> | |
$(@).addClass('dependent') | |
@each -> | |
$(@).data('pre', $(@).val()) | |
@disableOthers = -> | |
$('select.dependent') | |
.not($(@)) | |
.find("option[value=#{$(@).val()}]") | |
.prop('disabled', true) | |
@disableOthers() | |
$(@).on 'change', (e) => | |
$target = $(e.currentTarget) | |
prev = $target.data('pre') | |
$("select.dependent option[value=#{prev}]") | |
.prop('disabled', false) | |
@disableOthers() | |
$target.data('pre', $target.val()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it out on this here