使用 jQuery 访问 Select 标签元素值
Access Select tag elements values using jQuery
我创建了一个具有三个值的枚举。
在创建视图文件时,Enum 显示为 Select 标签。
我想根据 Select 标签中所选元素的值隐藏和显示(切换)特定字段。
当我运行代码和改变选择的项目时,(SlideDown)函数自动执行。但我希望该函数仅在所选项目的值等于 2 时执行。(不是在我更改时)
这是创建视图文件中的 Select 标记部分:
<div class="form-group">
<label asp-for="Certificate" class="control-label"></label>
<select id="crfct" asp-for="Certificate" asp-items="Html.GetEnumSelectList<Certificate>()" class="form-control"></select>
<span asp-validation-for="Certificate" class="text-danger"></span>
</div>
<div class="form-group" id="crfctcost">
<label asp-for="CertificateCost" class="control-label"></label>
<input asp-for="CertificateCost" class="form-control" />
<span asp-validation-for="CertificateCost" class="text-danger"></span>
</div>
和jQuery代码:
<script>
$(document).ready(function () {
$("#crfctcost").hide();
$("#crfct").change(function () {
var selectedCertificate = $('#crfct').find(":selected").val();
if (selectedCertificate == "2"); {
$("#crfctcost").slideDown();
}
});
});
</script>
希望这对您有所帮助。你添加了一个';'如果
$(document).ready(function () {
$("#crfctcost").hide();
$(document).on('change','#crfct',function (e) {
var selectedCertificate = $(this).val();
if (selectedCertificate == 2){
$("#crfctcost").slideDown();
}
});
});
我创建了一个具有三个值的枚举。 在创建视图文件时,Enum 显示为 Select 标签。
我想根据 Select 标签中所选元素的值隐藏和显示(切换)特定字段。
当我运行代码和改变选择的项目时,(SlideDown)函数自动执行。但我希望该函数仅在所选项目的值等于 2 时执行。(不是在我更改时)
这是创建视图文件中的 Select 标记部分:
<div class="form-group">
<label asp-for="Certificate" class="control-label"></label>
<select id="crfct" asp-for="Certificate" asp-items="Html.GetEnumSelectList<Certificate>()" class="form-control"></select>
<span asp-validation-for="Certificate" class="text-danger"></span>
</div>
<div class="form-group" id="crfctcost">
<label asp-for="CertificateCost" class="control-label"></label>
<input asp-for="CertificateCost" class="form-control" />
<span asp-validation-for="CertificateCost" class="text-danger"></span>
</div>
和jQuery代码:
<script>
$(document).ready(function () {
$("#crfctcost").hide();
$("#crfct").change(function () {
var selectedCertificate = $('#crfct').find(":selected").val();
if (selectedCertificate == "2"); {
$("#crfctcost").slideDown();
}
});
});
</script>
希望这对您有所帮助。你添加了一个';'如果
$(document).ready(function () {
$("#crfctcost").hide();
$(document).on('change','#crfct',function (e) {
var selectedCertificate = $(this).val();
if (selectedCertificate == 2){
$("#crfctcost").slideDown();
}
});
});