2016-03-05 70 views
-3

可能很简单。当我在创建的窗体中点击提交按钮时,我应该在控制台上看到“已单击”消息,但未显示。任何想法为什么发生这种情况console.log不起作用

$("#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>

+1

**任何想法,为什么你没有添加jQuery?** –

+0

@ aptbs85你的代码看起来不是在'$(document).ready();'调用中,因此点击事件在页面加载时不会被绑定。 – Daedalus

回答

0

$功能仅由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的作品在这里。

+0

感谢您的回复,jquery已经添加到了我的index.html的底部,但我没有复制它,我只是包含了受jq影响的html。仍然不工作,虽然 – aptbs85

+0

@ aptbs85请参阅片段。它在这里工作。所以这个问题在这里是无法重现的,你同意吗? –

+0

@ aptbs85你在'$(function(){})'里面添加了你的代码吗? –

相关问题