简单的 JQuery 函数在表单提交后不会执行



简单的JQuery函数在表单提交后不会执行。我可以看到我的表单值已发送,但是当函数达到时,没有执行。 我已经阅读了几篇帖子并从我的按钮 ID 中删除了"提交"值,但 JQuery 仍然可以工作。 任何帮助方向将不胜感激。

$(document).ready(function() {
//When the chat form is submitted
$("#chatform").submit(function(event) {
event.preventDefault();
//Take the values directly form the chat form//
var name = $("#uname").val();
var message = $("#message").val();
if (message != "" && name != "") {
$.post("clashofcolchatdata.php", {
message: message,
name: name
}, function(data) {
$("#chat_space").append(data);
});
} else {
alert("Data missing");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="chatform" action="">
<div id="chat_comform">
<!-- This is the blank space for the chat -->
<div id="chat_space">
</div>
<div id="chat_message">
<!--Name | Name of the user entering the message  -->
<input type="text" placeholder=" Name" name="uname" id="uname" style="color:black" required/>
<br />
<!--Message | This is the chat message -->
<input type="text" placeholder=" Enter your chat message" name="message" id="message" style="color:black" required/>
<!-- This disables the button if anything on the is still invalid -->
<button type="submit" id="chat_submit" name="formsub" value="post" style="color:black">Submit</button>
</div>
</div>
</form>

更改<form name="chatform"><form id="chatform">.执行$('#chatform')时,您正在按 html id 进行查询,但尚未在表单上设置 id。

最新更新