获取最近下拉列表的值

huangapple go评论40阅读模式
英文:

Getting the value of the closest dropdown list

问题

我有3个单选按钮,这些按钮是通过foreach循环创建的。这3个单选按钮中的2个将具有一个作为select2下拉框的文本框。当用户点击“Continue”时,我希望获取与选中的单选按钮最接近的下拉列表(select)中的值。出于测试目的,我只是尝试弹出该值。然而,当我点击“Continue”时,弹出框中显示“Undefined”。任何帮助将不胜感激。

foreach (var formEType in Model.FormETypes)
{
    <div class="card mb-3">
        <div class="card-body">
            <div class="form-check d-flex align-items-center">
                @Html.RadioButtonFor(m => m.FormETypeId, formEType.FormETypeId, new { @class = "form-check-input" })
                <label class="form-check-label edit-item">
                    <b>@formEType.Name</b><br />
                    <span class="text-muted">
                        @Html.Raw(formEType.Description)
                    </span>
                </label>
            </div>
        </div>

        @if (formEType.FormETypeId != FormETypeCode.Appointment)
        {
            <div class="form-group col-md-4">
                <label class="font-weight-bold">Officer</label>
                <select name="IndividualId" class="form-control search"></select>
            </div>
        }
    </div>
}
<button class="btn btn-primary" type="button" id="continue">Continue</button>

Javascript:

$('#continue').on('click', function () {
    var checkedRadio = $('input[name=FormETypeId]:checked', '#FormE');

    if (checkedRadio.val() == '@FormETypeCode.Separation' || checkedRadio.val() == '@FormETypeCode.StatusChange') {
        var closestOfficer = checkedRadio.nextAll('input').val();

        alert(closestOfficer);
    }
});
英文:

I have 3 radiobuttons that are created from a foreach loop. 2 out of the 3 radiobuttons will have a textbox that is a select2 dropdown. When the user clicks continue I want to get the value from the closest dropdown list (select) to the checked radiobutton. For testing purposes I'm just trying to alert the value. However, when I click Continue Undefined is appearing in the alert. Any help would be appreciated.

foreach (var formEType in Model.FormETypes)
{
<div class="card mb-3">
<div class="card-body">
<div class="form-check d-flex align-items-center">
@Html.RadioButtonFor(m => m.FormETypeId, formEType.FormETypeId, new { @class = "form-check-input" })
<label class="form-check-label edit-item">
<b>@formEType.Name</b><br />
<span class="text-muted">
@Html.Raw(formEType.Description)
</span>
</label>
</div>
</div>
@if (formEType.FormETypeId != FormETypeCode.Appointment)
{
<div class="form-group col-md-4">
<label class="font-weight-bold">Officer</label>
<select name="IndividualId" class="form-control search"></select>
</div>
}
</div>
}
<button class="btn btn-primary" type="button" id="continue">Continue</button>

Javascript:

$('#continue').on('click', function () {
var checkedRadio = $('input[name=FormETypeId]:checked', '#FormE');
if (checkedRadio.val() == '@FormETypeCode.Separation' || checkedRadio.val() == '@FormETypeCode.StatusChange') {
var closestOfficer = checkedRadio.nextAll('input').val();
alert(closestOfficer);
}
});

答案1

得分: 1

尝试这个方法:

var checked = $("input[type='radio']:checked");
var textbox = checked.closest("div").next('div').find('select');
英文:

Try this method:

 var checked = $("input[type='radio']:checked");
var textbox = checkedRadio.closest("div").next('div').find('select');

huangapple
  • 本文由 发表于 2023年2月16日 04:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定