【发布时间】:2026-01-30 19:40:01
【问题描述】:
我需要获取我在模态输入字段中输入的值,当我按下回车时,模态将关闭,我输入的输入值将显示在模态之外的另一个输入字段中。
$('#myModal2').on('keydown', function(e)
{
if (e.which == 13)
{
e.preventDefault();
var value = $(this).find('td:eq(myInput2)').text();
document.getElementById("my_member").value = value;
$("[data-dismiss=modal]").trigger(
{
type: "click"
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- modal button -->
<button type="button" class="btn btn-outline-secondary" style="background-
color:#e6e6e6; " data-toggle="modal" data-target="#myModal2"
id="btnproduct2" name="btnproduct2">...</button>
<!-- input field inside modal -->
<div class="modal fade" id="myModal2" role="dialog">
<input type="text" class="form-control" placeholder="ID..." id="myInput2"
autocomplete="off" />
</div>
<!-- input field outside modal -->
<input type="text" class="form-control" name="my_member" style="width:75%;"
id="my_member" value="" autocomplete="off">
【问题讨论】:
-
为什么不直接在
keydown回调中更新模式?
标签: javascript jquery html