我有一个聊天框,我正在使用onclick属性来调用函数。我只想帮助使用javascript按Enter键来调用此函数。
<input id="Submit" class="btn btn-primary btn-lg" type="button" value="Send Message" onclick="set_chat_msg()"/>
使用Javascript:
function set_chat_msg()
{
if(typeof XMLHttpRequest != "undefined")
{
oxmlHttpSend = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
oxmlHttpSend = new ActiveXObject("Microsoft.XMLHttp");
}
if(oxmlHttpSend == null)
{
alert("Browser does not support XML Http Request");
return;
}
var url = "chat_send_ajax.php";
var strname="noname";
var strmsg="";
if (document.getElementById("txtname") != null)
{
strname = document.getElementById("txtname").value;
document.getElementById("txtname").readOnly=true;
}
if (document.getElementById("txtmsg") != null)
{
strmsg = document.getElementById("txtmsg").value;
document.getElementById("txtmsg").value = "";
}
url += "?name=" + strname + "&msg=" + strmsg;
oxmlHttpSend.open("GET",url,true);
oxmlHttpSend.send(null);
}
答案 0 :(得分:0)
我对此有所了解。
我使用了这个javascript:
function checkEnter(e){
var characterCode
if(e && e.which){
e = e
characterCode = e.which
}
else {
e = event
characterCode = e.keyCode
}
if(characterCode == 13){
set_chat_msg();
return false
}
else{
return true
}
}
然后我在文本区onKeyPress="return checkEnter(event)"