gpt4 book ai didi

javascript 捕获一个输入

转载 作者:行者123 更新时间:2023-12-02 23:47:25 24 4
gpt4 key购买 nike

我正在 try catch 此选择的更改:

 @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):

introducir la descripción de la imagen aquí

编辑 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>

列表类:

<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

最佳答案

将代码更改为

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>

关于javascript 捕获一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com