【问题标题】:unable to get jQuery .submit() to fire [closed]无法让 jQuery .submit() 触发 [关闭]
【发布时间】:2017-06-19 18:02:23
【问题描述】:

我无法让 jQuery .submit() 在我的表单上触发。如果我将它移动到 $(document).ready(),下面的警报会成功弹出,但试图让它在提交时触发是行不通的。

在我的 BODY 中使用以下脚本

<script>
  $(document).ready(function($) {
    $('mainForm').submit(function() {
      alert("hello");
    });
  });
</script>

以及下面的表单元素

<form id="mainForm" class="form-horizontal" method="POST">
<legend>Retrieve Your Documents</legend>
<fieldset>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="id">Request ID</label>  
  <div class="col-md-4">
  <input id="requestid" name="requestid" type="text" placeholder="DOC123456-12345678901234" class="form-control input-md" required="">  
  </div>
</div>

<!-- Password input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="lastfour">Last Four of SSN</label>
  <div class="col-md-4">
    <input id="lastfour" name="lastfour" type="password" placeholder="1234" class="form-control input-md" required="">
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="button"></label>
  <div class="col-md-4">
    <input type="submit" id="button" name="button" class="btn btn-primary" value="Retrieve Documents">
  </div>
</div>

</fieldset>
</form>

【问题讨论】:

  • $('#mainForm'):你错过了#
  • 这成功了,但实际上我之前已经尝试过,但它不起作用。但是,我明确使用 HTML4 作为 DOCTYPE。当我切换到 HTML5 时,它按预期工作。

标签: jquery html bootstrap-4


【解决方案1】:

正如 haim770 所说,mainForm 是表单的 id,因此您需要在其前面加上 # 前缀,如下所示:

$('#mainForm').submit(function() { 
    alert("hello"); 
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多