問題:當我點擊按鈕類型=「按鈕」它顯示所有數據。但如果我將其更改爲type =「submit」,它不會顯示數據。我想要的是將此類型=「按鈕」轉換爲可顯示數據的type =「submit」。轉換類型按鈕提交與Ajax和jQuery
這裏是我的代碼:
<form name="chatform">
<div class="input-group" id="msg">
<input type="text" class="form-control" name="inputmsg" placeholder="Enter message here. . ." id="inputmsg">
<span class="input-group-btn">
<button class="btn btn-success" type="button" name="btn-send" id="cnd" onClick="submitChat()">Send</button>
</span>
</div>
</form>
AJAX瓦特/ jQuery的
function submitChat(){
if(chatform.inputmsg.value== ""){
alert("fill in blanks");
return;
}
var msg = chatform.inputmsg.value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status==200){
document.getElementById("chatlogs").innerHTML = xhttp.reponseText ? xhttp.reponseText : "";
}
};
xhttp.open("GET","insert.php?msg="+msg, true);
xhttp.send();
$(document).ready(function(){
$.ajaxSetup({cache:false});
setInterval(function(){
$("#chatlogs").load("logs.php");
}, 2000);
});
document.getElementById("inputmsg").value = "";
}
嘗試更改'function submitChat()'到'function submitChat(e)'並在函數內部的第一行運行'e.preventDefault();' – Phiter