添加enter以提交textarea

时间:2015-03-30 11:15:49

标签: javascript php jquery

您好我创建了一个新的聊天框,正在工作,但我需要当我点击输入以汇总消息(去功能Kucaj())。你可以帮助我吗? 我添加一些代码用于输入但没有工作。谢谢

    <?php
session_start();
if(!isset($_SESSION['username'])){
?>
<form name="forma2" action="login.php" method="post">
<table>
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" />
<tr>
<td colspan="2"><input type="submit" name"submit" value"Login"/td>
</tr>
<tr>
<td colspan="2"><a href="register.php">Registruj se!</a></td>
</tr>


<?php
exit;
}
?>

<html>
<head>
<title>Maturski rad</title>
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script>

function Klikni(){
        if(forma1.msg.value == ''){
                alert('Upisi poruku!');
                return;
            }
            var msg = forma1.msg.value;
            var xmlhttp = new XMLHttpRequest();

            xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState==4&&xmlhttp.status==200){
                            document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
                        }
 forma1.msg.value='';
                }
            xmlhttp.open('GET','insert.php?&msg='+msg,true);
            xmlhttp.send();
    }

    $(document).ready(function(e) {
        $.ajaxSetup({cache:false});
        setInterval(function() {$('#chatlogs').load('logs.php');}, 2000);
    });

</script>

</head>
<body>

<form name="forma1" action="#" id="forma1">

Username: <b><?php echo $_SESSION['username']; ?></b></br>
Poruka: <br />
<textarea name="msg" style="width:500px; height:100px"></textarea><br />

<a href="#" onclick="Klikni()"; class="button">Posalji</a><br /><br />
<a href="logout.php" class="button">Izloguj se</a><br /><br />
<script>
$("forma1").keypress(function(event) {
    if (event.which == 13) {
        event.Klikni();

    }}
    </script>
<div id="chatlogs">
Molim sacekajte da se ocita!!!
</div>


</body>
</html>

4 个答案:

答案 0 :(得分:0)

尝试这样source

form.submit(function(e) {
    e.preventDefault();              
    $(this).find('[type=submit]').click();
    $("form").submit();       
})

答案 1 :(得分:0)

尝试使用keydown而不是keypress:

$("forma1").keydown(function(event) {

答案 2 :(得分:0)

你可以这样使用。它应该是这样Klikni();而不是event.Klikni();

<script>
$("forma1").keypress(function(event) {
    if (event.which == 13) {
        Klikni(); //no need of event there..    
    }}
    </script>

答案 3 :(得分:0)

您只需在表单中添加onsubmit=return Klikni();属性,在return false; Klikni功能中添加.keypress()即可,而无需使用function Klikni(e) { alert("Enter pressed"); return false; }。用户按Enter键提交的表单是默认行为。

&#13;
&#13;
<form name="forma1" action="#" id="forma1" onsubmit="return Klikni();">
  Put your cursor into the field and press Enter.<br>
  <input type="text">
</form>
&#13;
{{1}}
&#13;
&#13;
&#13;