admin管理员组文章数量:1414887
Scenario: I am selling a T-shirt in pink, navy, black and size small, medium and large. I've got two selection boxes, one for color and the other for size. I'm sold out of medium pink T-shirts, so need to disable this option. How can I disable the 'Medium' option when 'Pink' is selected, but ensure that it's active for the other color options?
<select name="product[]" id="color">
<option value="Pink">Pink</option>
<option value="Navy">Navy</option>
<option value="Black">Black</option>
</select>
<select name="product[]" id="size">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>
I've had a punch at solving this issue myself but can't seem to get it to work. I need a solution which works with option values with letters and spaces i.e. 'Dark Pink' or 'Extra Large', as the option values are passed forward to the checkout. This is the closest I've gotten to a solution:
function enableElements()
{
if($('#color').val() == "pink"){
$("#size option[value='medium']").attr('disabled',true);
}
else $("#size option[value='medium']").attr('disabled',false);
}
^ This is based upon the article found here: /.
Scenario: I am selling a T-shirt in pink, navy, black and size small, medium and large. I've got two selection boxes, one for color and the other for size. I'm sold out of medium pink T-shirts, so need to disable this option. How can I disable the 'Medium' option when 'Pink' is selected, but ensure that it's active for the other color options?
<select name="product[]" id="color">
<option value="Pink">Pink</option>
<option value="Navy">Navy</option>
<option value="Black">Black</option>
</select>
<select name="product[]" id="size">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>
I've had a punch at solving this issue myself but can't seem to get it to work. I need a solution which works with option values with letters and spaces i.e. 'Dark Pink' or 'Extra Large', as the option values are passed forward to the checkout. This is the closest I've gotten to a solution:
function enableElements()
{
if($('#color').val() == "pink"){
$("#size option[value='medium']").attr('disabled',true);
}
else $("#size option[value='medium']").attr('disabled',false);
}
^ This is based upon the article found here: http://www.codeoncall./disable-drop-down-list-items-based-on-another-drop-down-selection/.
Share Improve this question asked Sep 25, 2014 at 13:14 charbycharby 131 silver badge2 bronze badges 3- 1 possible duplicate of How to disable select based on other selected option? – emerson.marini Commented Sep 25, 2014 at 13:16
-
Just a tip anyway. Don't use
.attr()
for this. Use.prop()
instead. .prop() vs .attr() – emerson.marini Commented Sep 25, 2014 at 13:17 -
Another tip: You don't actually need an
if...then...else
for this.$("#size option[value='medium']").prop('disabled', $('#color').val() == "pink");
– emerson.marini Commented Sep 25, 2014 at 13:18
3 Answers
Reset to default 3Try this:
$('#color').change(function(e){
if($(this).val() == "Pink"){
$("#size option[value='Medium']").prop('disabled',true);
}
else {
$("#size option[value='Medium']").prop('disabled',false);
}
});
Use 'prop' instead of 'attr'.
Demo:
http://jsfiddle/59knw46r/
Here is an alternative method:
function enableElements() {
if ($('#color').val() == "pink") {
$("#size").children().each(function() {
if ($(this).val() == 'medium') {
$(this).prop('disabled',true);
}
});
} else {
$("#size").children().each(function() {
if ($(this).val() == 'medium') {
$(this).prop('disabled',false);
}
});
}
}
a better , or i can say a faster approach would be as following snippet.
$("#size").find("option[value='Medium']").prop('disabled',true)
本文标签: javascriptHow to disable select option based on another select optionStack Overflow
版权声明:本文标题:javascript - How to disable select option based on another select option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745181282a2646477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论