【问题标题】:How to find the value that is typed in the modal input and change the value of another input when enter key is pressed如何找到在模态输入中键入的值并在按下回车键时更改另一个输入的值
【发布时间】: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


【解决方案1】:

更好地处理提交按钮上的点击事件。然后在 Enter Pressing 时触发 Button Click 事件。

Trigger an event on `click` and `enter`

关于关闭模态后更改文本值。您应该使用 jquery 方法而不是 javascript。

jQuery change input text value

下次,请在询问之前尝试更多。干杯。

P.s:如果将 jquery 添加到页面,使用 jquery 方法而不是 javascript。

【讨论】:

    最近更新 更多