【问题标题】:Open bootstrap modal when enter pressed on form在表单上按下输入时打开引导模式
【发布时间】:2020-12-10 12:46:22
【问题描述】:

我创建了一些在提交时会打开模式框的表单。每当单击提交按钮时,它们都可以正常工作,但是如果有人只是在输入框中按 Enter,则它不起作用。

我尝试将模态选项添加到输入框,但只要它们被选中就会打开模态。

这是我的一种表单的 html:

<form method="POST" id="new_group">{% csrf_token %}
    <div class="form-group row justify-content-center">
        <div class="col-10">
            <input type="text" class="form-control border" id="name" name="name" placeholder="Name...">
        </div>
        <button type="button" id="new_btn" class="btn btn-viso" value="new_group" form="new_group" onclick="setSubmitValue('new_btn')" data-toggle="modal" data-target="#confirmationModal"><i class="far fa-save"></i></button>
    </div>
</form>

谢谢!

【问题讨论】:

标签: javascript html bootstrap-4 bootstrap-modal


【解决方案1】:

您可以在提交到表单时添加事件处理程序。因此,无论选择哪种提交类型(输入或按钮单击),模式都会打开。这应该会为您指明正确的方向。

function submit_handler() {
  console.log("modal opened"); //send with ajax?
  $('#exampleModal').modal('show');
  event.preventDefault();
}

function submitform() {
  $('#new_group').submit();
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<form method="POST" id="new_group" onsubmit="submit_handler()">
  <div class="form-group row justify-content-center">
    <div class="col-10">
      <input type="text" class="form-control border" id="name" name="name" placeholder="Name...">
      <input type="submit" id="new_btn" class="btn btn-info">
    </div>
  </div>
</form>

<div class="modal" tabindex="-1" role="dialog" style="display:none" id="exampleModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" onclick="submitform()">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

【讨论】:

  • 这会很好地打开模式,但一旦按下“保存更改”,则什么也不做。
  • 因为这不是您对@scottapotamus 的要求,所以我编辑了我的 sn-p。如果您按“保存更改”,则会发送表单。
  • 传奇!谢谢!
【解决方案2】:

如果您希望 enter 键默认工作,您应该在按钮中使用 type="submit",但我认为您是通过 ajax 提交它,所以这是不可能的(不再推荐)。也就是说,您需要在所有输入元素上监听 keyup 事件,并在按下键时触发您的提交函数。

$("input").on('keyup', function (e) {
    if (e.key === 'Enter' || e.keyCode === 13) {
        // Function to submitting the form and or opening the modals here
    }
})

反正我不明白你是如何管理这里的表单提交的。那个 onclick 函数有什么作用?看起来该函数修改了按钮本身的某些内容。您是尝试通过 ajax 还是像旧的标准方式一样提交表单?

【讨论】:

  • 这个答案绝对不是最好的方法。有一个可以绑定到表单的 onSubmit 事件。在大多数情况下,收听事件键码并不是您能做的最好的事情。
  • 我的建议是实现请求者代码中保留内容的最佳方式,否则这个问题应该标记为重复然后关闭。关于如何提交表单的最佳实践有大量的教程和更好的答案,我们不需要重新发明轮子。但是,再一次,因为:问题包含一个模棱两可的 onclick 函数并且表单按钮不是 type=submit,我只是给了我的 2 美分,将用户指向 keyup 事件,这无疑是与键盘交互的最佳方式。
  • 是的,你可能是对的@Mauro。也许OP应该就他的问题提供更多细节。
  • 由于某些页面上有多个表单,我需要告诉 python 哪个表单已提交,以便它运行正确的脚本。 onclick 函数只是将该信息添加到模态提交按钮。模态框内置在模板中,因此在所有页面中都是标准的。在按下该按钮之前,它不包含任何信息。
【解决方案3】:

你可以试试这个……

<script>
    $(document).ready(function(){
     
        $('form').on('submit',function(e){
           $('div#myModal').modal('open')
        })
    })
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-08
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多