我正忙着使用AJAX,jQuery和HTML 5上学。我需要建立一个聊天室,并且除了发布帖子的这一部分之外,其他所有功能都可以正常工作。
这是表格的代码:
<form id="chatForm">
<div id="chatHead">
<input type="text" name="chatName" id="userName" placeholder="Username" required="required" />
<input type="button" value="Start" name="chatButton" id="chatButton" />
</div>
<div id="chatBox"></div>
<div id="chatFoot">
<input type="text" name="chatMessage" id="chatMessage" size="63" placeholder="Bericht" />
<input type="submit" name="chatSend" id="chatSend" value="Send" disabled="disabled" />
</div>
</form>
这是插入帖子的功能:
function postChat() {
$(chatForm).submit(function () {
return false;
});
if ($(chatForm)[0].checkValidity()) {
if (!this.checkValidity()) {
}
else {
var Naam = $(chatName).val(),
Bericht = $(chatMessage).val();
$.get(phpScript, {action: "addMessage", name: Naam, message: Bericht})
.done(function () {
postChat();
startChat("system");
});
$(chatMessage).val("");
}
}}
我总是收到此错误: Uncaught TypeError: undefined is not a function
它说这部分代码出错了:
if (!this.checkValidity()) {
}
我不知道这到底意味着什么,但这是为了确保系统不会继续发布消息。
如果有人也可以解释我的确切意思,我将非常优雅。
您认为“这个”一词是什么?
if (!this.checkValidity()) {
}
也许您最好使用以下框架重写逻辑:
注意:从“提交”按钮中删除disabled =“ disabled”。
$(document).ready(function(){
/* Run code only when DOM is ready (document loaded) */
$('#chatForm').on('submit', function(e) {
/* Run code when user sumbits the form (pressing button or ENTER */.
e.preventDefault(); // <-- prevent form to be sended
/*
NOW HERE DO VALIDATION AND AJAX POSTS
As example I inserted part of your code.
*/
if ($('#chatForm')[0].checkValidity()) {
var Naam = $('#userName').val(), Bericht = $('#chatMessage').val();
$.get(phpScript, {action: "addMessage", name: Naam, message: Bericht})
.done(function () {
startChat("system");
});
$(chatMessage).val("");
} else {
alert("Validation failed");
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句