简体   繁体   English

选择选项 x 次时从下拉列表中禁用选项

[英]Disable option from dropdown when option is selected x times

I have a dropdown menu in form that calculates how many times option has been selected.我有一个下拉菜单,用于计算选择了多少次选项。 I want to disable option when option has been selected x times.选择选项x次时,我想禁用选项。 This is my code (nth iteration. I suck at coding.) that disables every option.这是我的代码(第 n 次迭代。我不擅长编码。)禁用每个选项。

var x = 10
var time_1 = parseInt(document.getElementById("time-1").value)
var time_2 = parseInt(document.getElementById("time-2").value)
var time_3 = parseInt(document.getElementById("time-3").value)

$("#id").children().each(function() {
     if (time_1 >= x) {
         $(this).prop("disabled", "true");
     }
});

After a good nights sleep and a cup of good coffee I managed to find a solution.经过一夜好眠和一杯好咖啡,我设法找到了解决办法。 I know this is not pretty code but it works for me.我知道这不是漂亮的代码,但它对我有用。 I might have been a bit obscure about what I was aiming for.我可能对我的目标有点模糊。 Sorry about that.对于那个很抱歉。

$(function (){
var x = 10
var time_1 = parseInt(document.getElementById("time-1").value)
var time_2 = parseInt(document.getElementById("time-2").value)
var time_3 = parseInt(document.getElementById("time-3").value)

if (time_1 > x) { $('#id option[value="Option value 1"]').attr('disabled','disabled'); } 
if (time_2 > x) { $('#id option[value="Option value 2"]').attr('disabled','disabled'); }
if (time_3 > x) { $('#id option[value="Option value 3"]').attr('disabled','disabled'); } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM