【问题标题】:console.log does not work [duplicate]console.log 不起作用[重复]
【发布时间】:2016-03-05 21:46:23
【问题描述】:

可能很简单。当我单击我创建的表单中的提交按钮时,我应该在控制台上看到“单击”消息,但它没有出现。任何想法为什么会发生这种情况?

$("#button").on("click", function() {
  console.log("clicked");
  return false;
});
<div class="col-md-8">
  <form>
    <div class="form-group">
      <label for="name" class="sr-only">Name</label>
      <input type="text" class="form-control" id="name" placeholder="Your name here" maxlength="20">
    </div>
    <div class="form-group">
      <label for="email" class="sr-only">Email</label>
      <input type="email" class="form-control" id="email" placeholder="Your email here*" required="required">
    </div>
    <label for="textBox" class="sr-only">TextBox</label>
    <textarea class="form-control message-box" id="textBox" style="resize:none" cols="40" rows="5" placeholder="Your message here" minlength="20"></textarea>
    <button type="submit" class="btn btn-default" id="button">Submit</button>

  </form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

【问题讨论】:

  • 知道吗,为什么不添加 jQuery?
  • @aptbs85 您的代码似乎不在$(document).ready(); 调用中,因此在页面加载时不会绑定点击事件。

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

$ 函数仅由 jQuery 定义。您需要包含 jQuery 才能使其正常工作。

$("#button").on("click", function() {
  console.log("clicked");
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-8">
  <form>
    <div class="form-group">
      <label for="name" class="sr-only">Name</label>
      <input type="text" class="form-control" id="name" placeholder="Your name here" maxlength="20">
    </div>
    <div class="form-group">
      <label for="email" class="sr-only">Email</label>
      <input type="email" class="form-control" id="email" placeholder="Your email here*" required="required">
    </div>
    <label for="textBox" class="sr-only">TextBox</label>
    <textarea class="form-control message-box" id="textBox" style="resize:none" cols="40" rows="5" placeholder="Your message here" minlength="20"></textarea>
    <button type="submit" class="btn btn-default" id="button">Submit</button>

  </form>

添加 jQuery 在这里工作。

【讨论】:

  • 感谢您的回复,jquery 已经添加到我的 index.html 底部但我没有复制它,我只是包含了受 jq 影响的 html。不过还是不行
  • @aptbs85 查看 sn-p。它在这里工作。所以这个问题在这里无法重现,你同意吗?
  • @aptbs85 您是否在$(function () {}) 中添加了代码?
猜你喜欢
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 2021-03-28
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多