你好,我有一个datalist和一些带有list的输入。我无法获取选定的输入id
我试过了,但没用。
$(".updateResolutions").click(function () {
let regDate = $(this).closest('div').find('#regDate').val();
var opt = $('.Organizations option[value="' + $(this).val() + '"]');
console.log(opt.attr('id'));
console.log(opt);
);
});
这是html
<datalist id="OrganizationsList">
<option id="1" value="Name1" />
<option id="2" value="Name2" />
</datalist>
<div class="col-md-11 borderDiv">
<input type="date" name="regdate" id="regDate" />
<label for="regdate"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" class="resOrgId Organizations" value="@item.OrgName" style="width:30em" />
<button type="button" id="updateResolutions" class="btn btn-default updateResolutions">изменить</button>
</div>
<div class="col-md-11 borderDiv">
<input type="date" name="regdate" id="regDate" />
<label for="regdate"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" class="resOrgId Organizations" value="@item.OrgName" style="width:30em" />
<button type="button" id="updateResolutions" class="btn btn-default updateResolutions">изменить</button>
</div>
div可以大于2 (3-10)
发布于 2019-10-10 15:34:25
您可以通过它的值选择选项:$('#OrganizationsList option[value="' + $(this).siblings('input[name=resOrg]').val() + '"]')
。
$(function(){
$(".updateResolutions").click(function (e) {
var opt = $('#OrganizationsList option[value="' + $(this).siblings('input[name=resOrg]').val() + '"]').attr('id');
console.log(opt);
});
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<datalist id="OrganizationsList">
<option id="1" value="Name1" />
<option id="2" value="Name2" />
</datalist>
<div class="col-md-11 borderDiv">
<label for="regdate1"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" id="" value="Name1" style="width:30em" />
<button type="button" id="regdate1" class="btn btn-default updateResolutions">изменить</button>
</div>
<div class="col-md-11 borderDiv">
<label for="regdate2"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" id="" value="Name2" style="width:30em" />
<button type="button" id="regdate2" class="btn btn-default updateResolutions">изменить</button>
</div>
此外,id
值在按钮和其他输入中应该是唯一的。
发布于 2019-10-10 15:26:47
您的代码中几乎没有错误
id应该是唯一的,正如我在代码中看到的那样,resOrgId使用了两次
你有额外的);在JS
下面的代码应该很适合您
$('.updateResolutions').click(function(){
let regDate = $(this).closest('div').find('#regDate').val();
alert(regDate)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<datalist id="OrganizationsList">
<option id="1" value="Name1" />
<option id="2" value="Name2" />
</datalist>
<div class="col-md-11 borderDiv">
<input type="date" name="regdate" id="regDate" />
<label for="regdate"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" id="resOrgId" value="@item.OrgName" style="width:30em" />
<button type="button" id="updateResolutions" class="btn btn-default updateResolutions">изменить</button>
</div>
<div class="col-md-11 borderDiv">
<input type="date" name="regdate" id="regDate" />
<label for="regdate"> Организация : </label>
<input type="text" list="OrganizationsList" name="resOrg" id="resOrgId" value="@item.OrgName" style="width:30em" />
<button type="button" id="updateResolutions" class="btn btn-default updateResolutions">изменить</button>
</div>
https://stackoverflow.com/questions/58317137
复制相似问题