admin管理员组文章数量:1392076
I have two select elements. both select have same values. when I select option from 1 select box it should disable all previous select options from second select box. Let's suppose I have this select:
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="s2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If I select value 3 from s1 box it should disable all previous options from s2 select. if I select value 2 from s1 it should disable all previous values in s2.
Note: It should not disable next values, just the previous ones. I'm looking for a jquery
solution.
I have two select elements. both select have same values. when I select option from 1 select box it should disable all previous select options from second select box. Let's suppose I have this select:
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="s2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If I select value 3 from s1 box it should disable all previous options from s2 select. if I select value 2 from s1 it should disable all previous values in s2.
Note: It should not disable next values, just the previous ones. I'm looking for a jquery
solution.
-
How about
:lt
selector ? – Rayon Commented Mar 10, 2016 at 6:19 - can you please give more details regarding :lt? a guide to implement :lt in my case? – Mossawir Ahmed Commented Mar 10, 2016 at 6:21
2 Answers
Reset to default 8Use :lt
to select elements having lesser index than specified.
.index()
will return position of the element from the matched element.
.not()
will exclude specified element from the returned elements.
$('select').change(function() {
$('select option').prop('disabled', false);
var index = $(this).find('option:selected').index();
$('select').not(this).find('option:lt(' + index + ')').prop('disabled', true);
})
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="s2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Fiddle here
Try below code If it is useful for you
$(document).ready(function() {
$("#s1").change(function() {
var optVal = $(this).val();
$("#s2 option").each(function() {
$(this).prop('disabled', false);
if (optVal > $(this).val()) {
$(this).prop('disabled', true);
}
});
});});
版权声明:本文标题:javascript - How to disable previous selected options when select option from first select - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744767031a2624104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论