为什么当我点击表单内的按钮时提交块永远不会到达?
why the submit block is never reached when i click button inside form?
我正在用 nodejs 创建一个聊天应用程序。
在 index.html 文件中,我设置了用户发送消息的界面,我定义了以下形式:
<form id="chat-form" name="chat-form">
<input type="text" name="chat-input" id="chat-input" placeholder="type message">
<br>
<input type="submit" value="send"/>
</form>
在 <head> </head>
标签内,我使用以下脚本:
$('#chat-form').submit(function(e){
console.log("inside the submit");
});
尽管此代码预期有效,但从未达到提交代码。
如果您在 head
中调用您的代码,那么代码会在 html 呈现之前得到 运行,因此此时 html 表单将不存在.
一般的最佳做法是将 javascript 代码放在文件的底部,就在结束 body
标记 and/or 到 运行 之前dom 就绪功能。
$(function(){ [code]});
一旦 dom 加载,上述函数中的任何代码 运行 都将 运行,因此您的 html 将 在那一点存在。
$(function(){
$('#chat-form').submit(function(e){
console.log("inside the submit");
});
});
我正在用 nodejs 创建一个聊天应用程序。
在 index.html 文件中,我设置了用户发送消息的界面,我定义了以下形式:
<form id="chat-form" name="chat-form">
<input type="text" name="chat-input" id="chat-input" placeholder="type message">
<br>
<input type="submit" value="send"/>
</form>
在 <head> </head>
标签内,我使用以下脚本:
$('#chat-form').submit(function(e){
console.log("inside the submit");
});
尽管此代码预期有效,但从未达到提交代码。
如果您在 head
中调用您的代码,那么代码会在 html 呈现之前得到 运行,因此此时 html 表单将不存在.
一般的最佳做法是将 javascript 代码放在文件的底部,就在结束 body
标记 and/or 到 运行 之前dom 就绪功能。
$(function(){ [code]});
一旦 dom 加载,上述函数中的任何代码 运行 都将 运行,因此您的 html 将 在那一点存在。
$(function(){
$('#chat-form').submit(function(e){
console.log("inside the submit");
});
});