Javascript / Php Chat - Enter在一个文本框中不起作用

时间:2013-12-14 02:33:38

标签: javascript php html

所以我和文本框聊了聊。如果我按ENTER键,聊天会发送消息。这很好,但在“警告”的事情,这是行不通的。当我按Enter键时,它不起作用。有人能帮我吗?

//This is for normal users:

    <form name="message" action="">
            <input name="usermsg" type="text" id="usermsg" size="63" />
            <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
    </form>
    <br>



//This is for moderators:

            <form name="moderating" method="post" action="">
            <hr align="center">
            <br>
            <p><b>Write in color:</b></p>
            <script type="text/javascript" src="color_picker/jscolor.js"></script>Select color: <input class="color" name="color_value" type="text" id="color_value" value="FF0F0F">
            <input name="colormsg" type="text" id="colormsg" size="25" />
            <input name="submitcolormsg" type="submit"  id="submitcolormsg" value="Send" />
            <br><br>
            <p><b>Warning</b></p>


//Is this wrong?! :

            <input name="warningmsg" type="text" id="warningmsg" size="63" />
            <input name="submitwarningmsg" type="submit"  id="submitwarningmsg" value="Warning" />
            <br><br>
            <p><b>Clear chat log</b></p>
            <input name="clear_button" type="submit"  id="clear_button" value="Clear" />
            <br><br>
            <p><b>Turn on/off chat</b></p>
            <input type="radio" name="chat_status_group" id="on" value="on" checked><b>On</b> -<input type="radio" name="chat_status_group" id="off" value="off"><b>Off</b> <input name="chat_status_button" type="submit"  id="chat_status_button" value="Ok" />
    </form>

   </div>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
   <script type="text/javascript">
   // jQuery Document
   $(document).ready(function(){

    $("#submitmsg").click(function(){       
            var clientmsg = $("#usermsg").val();
            $.post("***.php", {text: clientmsg});                               
            $("#usermsg").attr("value", "");
            return false;
    });

    $("#submitcolormsg").click(function(){       
            var clientcolormsg = $("#colormsg").val();
            var color = $("#color_value").val();
            $.post("***.php", {
            text: clientcolormsg,
            color: color
            });                       
            $("#colormsg").attr("value", "");
            return false;
    });

 //And here is the wrong?! :

    $("#submitwarningmsg").click(function(){       
            var post_warningmsg = $("#warningmsg").val();
            $.post("***.php", {text: post_warningmsg});                               
            $("#warningmsg").attr("value", "");
            return false;
    });



    $("#clear_button").click(function(){       
            $.post("clear.php");       
            return false;                       
    });



    $("#chat_status_button").click(function(){       
            var statusmsg = "on";
                    if(document.getElementById('on').checked) {
                    statusmsg = "on";
                    } else {
                    statusmsg = "off";
                    }
            $.post("chat_status.php", {
            text: statusmsg
            });                       
            return false;               
    });
   .
   .
   .


   });
   </script>
   </body>
   </html>

我认为问题在于:

$("#submitwarningmsg").click(function(){       
  var post_warningmsg = $("#warningmsg").val();
  $.post("***.php", {text: post_warningmsg});                               
  $("#warningmsg").attr("value", "");
  return false;
});

为什么

  

$(“#warningmsg”)。attr(“value”,“”);

不起作用?

1 个答案:

答案 0 :(得分:0)

它不起作用,因为您只检查点击,而不是提交。尝试将最后一个代码更改为:

$("#submitwarningmsg").submit(function(e){
  e.preventDefault();
  var post_warningmsg = $("#warningmsg").val();
  $.post("***.php", {text: post_warningmsg});                               
  $("#warningmsg").val("");
  return false;
});

此外,您忘记在最后一个'表单'中打开表单:

<form>