javascript - Html Input=text performs postback when enter is pressed -
i using signalr make chat , works when enters text , presses return
performs postback , previous chat lost , no message sent. primary goal avoid performing postback ideally call send button click event.
i have tried make javascript function on keypressed should cancel postback did not change anything. have tried change asp:textbox
autopostback attribute set false.
my current code:
<div class="container"> <input type="text" id="message" /> <input type="button" id="sendmessage" value="send"/> <ul id="discussion"> </ul> </div> <script src="scripts/jquery-1.6.4.min.js" ></script> <script src="scripts/jquery.signalr-2.0.2.min.js"></script> <script src="signalr/hubs"></script> <script type="text/javascript"> $(function () { // declare proxy reference hub. var chat = $.connection.chathub; // create function hub can call broadcast messages. chat.client.broadcastmessage = function (time, name, message) { // html encode display time, name, , message. var encodedtime = $('<div />').text(time).html(); var encodedname = $('<div />').text(name).html(); var encodedmsg = $('<div />').text(message).html(); // add message page. $('#discussion').append('<li>[' + encodedtime +'] ' + encodedname + ': ' + encodedmsg + '</li>'); }; // set initial focus message input box. $('#message').focus(); // start connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // call send method on hub. chat.server.send($('#message').val()); // clear text box , reset focus next comment. $('#message').val('').focus(); }); }); }); </script>
it appears html wrapped <form>
. should able prevent postbacks adding following js:
$('form').submit(function(event) { event.preventdefault(); });
you might want make selector more specific of course.
Comments
Post a Comment