简体   繁体   English

ASP.NET MVC Razor获取选择选项值

[英]ASP.NET MVC Razor Getting select option value

Here's my code. 这是我的代码。 Excuse me if I explain this wrong. 对不起,如果我解释这个错误。

<select class="form-control" data-val="true" data-val-number="The field 
SupervisorID must be a number." data-val-required="The SupervisorID field is 
required." id="SupervisorID" name="SupervisorID"><option value="" 
disabled="disabled">Select Supervisor</option>
<option value="5" disabled="disabled">Tom</option>
<option value="6" disabled="disabled">John</option>
</select>

My question is how do I access the option value. 我的问题是如何访问选项值。 I tried using 我尝试使用

   $('select[option[value*='+$val+']]').removeAttribute

I just want to remove the attribute from the value selected. 我只想从所选值中删除属性。 $val is a value that I select in a checkbox and that selected value gets enabled in the select list. $ val是我在复选框中选择的值,并且所选值在选择列表中启用。 So when I check value 5 Tom will have his disabled attribute removed. 因此,当我检查值5时,汤姆将删除其禁用的属性。 But this doesn't seem to work. 但这似乎不起作用。 And I tried using: 我尝试使用:

document.getElementByID('SupervisorID').options[$val].removeAttribute('disabled');

But when the value is 5 or 6 and there's only 2 option there isn't a 5th or 6th value in the list. 但是,当值是5或6且只有2个选项时,列表中没有第5或第6个值。 In this way when there's 2 values and I check the second one (John with the value 6) in the select list it selects the 6th value of the list, when there isn't one. 这样,当有2个值并且我在选择列表中检查第二个值(John的值为6)时,它将选择列表中的第6个值(如果没有)。

I would like to do this the first way, not with the options[$val] way, although any help is appreciated. 我想第一种方法,而不是使用options [$ val]方法,尽管可以得到任何帮助。 Thank you for your answers. 谢谢您的回答。

You can done using jquery. 您可以使用jquery完成。

  $('select option:selected').removeAttr('disabled'); $(".chk").on("change",function(){ if($(this).is(":checked")){ $('select option[value="' + $(this).attr('value')+ '"]').removeAttr('disabled'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <select class="form-control" data-val="true" data-val-number="The field SupervisorID must be a number." data-val-required="The SupervisorID field is required." id="SupervisorID" name="SupervisorID"> <option value="" disabled="disabled" selected="selected">Select Supervisor</option> <option value="5" disabled="disabled">Tom</option> <option value="6" disabled="disabled">John</option> </select> <input class="chk" type="checkbox" value="5" /> <input class="chk" type="checkbox" value="6" /> 

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

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