我正在尝试捕获此选择的更改:
@if (!Model.Id.Equals(null))
{
@Html.DropDownListFor(m => m.Modificar.IdClase, new SelectList(Model.ListaClases, "IdClase", "Clase", Model.Modificar.IdClase), new { @class = "input form-control input-lg", @required = "required", @style = "height: 40px; font-size:16px" })
}
else
{
@Html.DropDownListFor(m => m.ListaClases, new SelectList(Model.ListaClases, "IdClase", "Clase"), new { @class = "input form-control input-lg", @required = "required", @style = "height: 40px; font-size:16px" })
}
使用变量将其停用
var clases = $('#Id').val() != "" ? $('#Modificar_IdClase') : $('#ListaClases');
我在console.log(clases)
中看到Modificar_IdClase像输入一样被捕获,而不像选择那样被捕获。为什么会发生,我该如何解决?
console.log(clases)
:
编辑1
在Chrome DevTools中编写的html代码为:
Modificar_IdClase:
<select class="input form-control input-lg" id="Modificar_IdClase" name="Modificar.IdClase" required="required" style="height: 40px; font-size:16px">
<option selected="selected" value="1">Articulo principal</option>
<option value="2">Articulo auxiliar</option>
</select>
Lista类别:
<select class="input form-control input-lg" id="ListaClases" name="ListaClases" required="required" style="height: 40px; font-size:16px">
<option value="1">Articulo principal</option>
<option value="2">Articulo auxiliar</option>
</select>
问题出在DropDownListFor
上,它正在获取数据并获取input
而不是select
答案 0 :(得分:1)
将代码更改为
var clases = $('#Id').val() != "" ? $('#Modificar_IdClase').val() : $('#ListaClases').val();
由于$('#Modificar_IdClase')
返回DOM对象,如果要获取标记的类,可以使用$('#Modificar_IdClase').attr('class')
$(document).ready(function(){
console.log($('#Modificar_IdClase').val());
console.log($('#Modificar_IdClase').attr('class'));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id='Modificar_IdClase' class='input form-control input-lg'>
<option value="test">Test</option>
</select>