英文:
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');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论