admin管理员组文章数量:1404063
I've got a group of 16 select boxes in a php mail() form, and I want to disable an option if it's chosen in any other box. The order of options in the select boxes aren't the same, and the I need the value for the php mail. I want the user filling in the form to not be able to choose the same option twice or more.
<select name="box1" id="box1">
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
<select name="box2" id="box2">
<option value="Two">Two</option>
<option value="One">One</option>
<option value="Three">Three</option>
<option value="Life">Life</option>
</select>
<select name="box3" id="box3">
<option value="Life">Life</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
Also, further down the website, I want to do the same with another group of 8 select boxes - and they contain some of the same options, but then I want them all to be choosable from the beginning, until an option from one of them get picked.
I know too little about jQuery and JavaScript. Help?
I've got a group of 16 select boxes in a php mail() form, and I want to disable an option if it's chosen in any other box. The order of options in the select boxes aren't the same, and the I need the value for the php mail. I want the user filling in the form to not be able to choose the same option twice or more.
<select name="box1" id="box1">
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
<select name="box2" id="box2">
<option value="Two">Two</option>
<option value="One">One</option>
<option value="Three">Three</option>
<option value="Life">Life</option>
</select>
<select name="box3" id="box3">
<option value="Life">Life</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
Also, further down the website, I want to do the same with another group of 8 select boxes - and they contain some of the same options, but then I want them all to be choosable from the beginning, until an option from one of them get picked.
I know too little about jQuery and JavaScript. Help?
Share Improve this question edited Oct 4, 2017 at 17:45 M0nst3R 5,3031 gold badge28 silver badges38 bronze badges asked May 21, 2016 at 9:49 soulitudesoulitude 1211 gold badge2 silver badges12 bronze badges 2- so do you want any help on PHP? Don't spam tags – Thamilhan Commented May 21, 2016 at 9:50
- The PHP is working! So, it's just the jQuery and Javascript for getting this disabled option thing. – soulitude Commented May 21, 2016 at 9:51
2 Answers
Reset to default 4Use
.filter
to get option-elements having value similar as current value.
$('select').on('change', function() {
$('option').prop('disabled', false); //reset all the disabled options on every change event
$('select').each(function() { //loop through all the select elements
var val = this.value;
$('select').not(this).find('option').filter(function() { //filter option elements having value as selected option
return this.value === val;
}).prop('disabled', true); //disable those option elements
});
}).change(); //trihgger change handler initially!
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select name="box1" id="box1">
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
<select name="box2" id="box2">
<option value="Two">Two</option>
<option value="One">One</option>
<option value="Three">Three</option>
<option value="Life">Life</option>
</select>
<select name="box3" id="box3">
<option value="Life">Life</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
Fiddle Demo
Thanks, Rayon. I finally got it to work flawlessly with this piece of code:
<script type="text/javascript">
$('select[name*="box1"]').change(function(){
$('select[name*="box1"] option').attr('disabled',false);
$('select[name*="box1"]').each(function(){
var $this = $(this);
$('select[name*="box1"]').not($this).find('option').each(function(){
if($(this).attr('value') == $this.val())
$(this).attr('disabled',true);
});
});
});
</script>
本文标签: javascriptDisable ltselectgt option when selected in another ltselectgt boxStack Overflow
版权声明:本文标题:javascript - Disable <select> option when selected in another <select> box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744238412a2596668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论