简体   繁体   English

如何根据数组中的内容将“禁用”属性切换到下拉菜单?

[英]How do I toggle the `disable` attribute to my dropdown-menu base on what I have in my array?

I have a dropdown-menu of 5 options. 我有5个选项的下拉菜单。

<select id="dd">
 <option value="1"> 1 <option>
 <option value="2"> 2 <option>
 <option value="3"> 3 <option>
 <option value="4"> 4 <option>
 <option value="5"> 5 <option>
</select>

I have an array that I need to check it against my dropdown-menu value 我有一个数组需要对照我的下拉菜单值进行检查

var enable = [1,3,5];

I want to produce this 我想生产这个

<select id="dd">
  <option value="1"> 1 <option>
  <option value="2" disabled="disabled"> 2 <option>
  <option value="3"> 3 <option>
  <option value="4" disabled="disabled"> 4 <option>
  <option value="5" > 5 <option>
</select>

How can I disable my 2, and 4 option ? 如何禁用2和4选项?

I only want to enable 1,3 and 5 . 我只想启用1,3和5。

I'm a little stuck here. 我有点卡在这里。

$("#dd > option").each(function() {
    if(  inArray(this.val(),studentsArray ) !== -1 ){
        //prop the disabled attribute
    }
});

Can someone please give me a little push here ? 有人可以在这里给我一点推动力吗?

You can do it right in HTML like this: 您可以像这样在HTML中正确执行此操作:

<select id="dd">
  <option value="1"> 1 <option>
  <option value="2" disabled="disabled"> 2 <option>
  <option value="3"> 3 <option>
  <option value="4" disabled="disabled"> 4 <option>
  <option value="5" disabled="disabled"> 5 <option>
</select>

Note that disabled="disabled" is XHTML style. 请注意,disabled =“ disabled”是XHTML样式。 Plain old disabled by itself as others have posted will work fine too. 由其他人张贴的普通老残障人士也可以正常工作。

If your disabled options will always be the same, this will work. 如果您禁用的选项始终相同,那么它将起作用。 But if you want to use jQuery to disable all options not in the array, you can do this: 但是,如果要使用jQuery禁用数组中未包含的所有选项,则可以执行以下操作:

var array = [1, 3];
$('#dd option').prop('disabled', true); // Disables all
for (var i in array) { // Iterates over array
    $('#dd option[value="' + array[i] + '"]').prop('disabled', false); // Enable this one option
}

I am completely re-writing this answer of mine, because I have a much better answer. 我完全重写了我的这个答案,因为我有一个更好的答案。 To change whether an option is disabled or not, give it an Id, or if there are several options to be disabled, give them all the same class. 要更改某个选项是否被禁用,请给它提供一个ID,或者如果要禁用多个选项,请为它们提供相同的类。 For an Id, (one element to be disabled only) do this: 对于ID(仅禁用一个元素),请执行以下操作:

variableName=document.getElementById('id');
//where 'id' in the line above is the id of the option to be disabled
variableName.setAttribute('disabled, 'disabled');

or for a class, it is very similar 或一个班级,非常相似

optionstobechanged=document.getElementsByClassName('class');
//where 'class' in the line above is the name of the class given to the items to be disabled
for(i=0; i<optionstobechanged.length; i++){
    optionstobechanged[i].setAttribute('disabled', 'disabled');
}

note that this can be recycled for similar things. 请注意,这可以回收用于类似的事情。 The

setAttribute();

function takes two strings: the property/attribute to be changed(ie selected, disabled) and the value of it. 函数使用两个字符串:要更改的属性/属性(即,选中,禁用)及其值。 so 所以

.setAttribute('disabled', 'disabled');

comes out as 出来作为

disabled='disabled'

you can also apply this to selected, and other attributes. 您还可以将此属性应用于选定属性和其他属性。 Now, all you have to do is rig this up inside a function and call it. 现在,您所要做的就是在函数内部进行装配并调用它。

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

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