javascript - jQuery refuses to post after validation -
i've added validation jquery code check blank text fields before posts message doesn't seem working. no messages being posted @ wall. idea how can fix this?
html
<div id="header"> <div class="container"> <span id="text_header">virtual idea wall</span> </div> </div> <div id="main"> <div class="container"> <div id="chat"></div> </div> </div> <div id="footer"> <div class="container"> <p> <label for="title">please give idea title</label> <br /> <input type="text" id="title" name="title"/> </p> <p> <label for="message">please provide details of idea</label> <br> <input type="text" id="message" name="message"/> </p> <p> <input type="submit" id="sendmessage"> </p> </input> </div> </div>
jquery
jquery(function ($) { var socket = io.connect(); var $messageform = $('#sendmessage'); var $messagetitle = $('#title'); var $messagebox = $('#message'); var $chat = $('#chat'); $messageform.click(function () { if ($.trim($("#title").val()).length === 0) { alert('you must provide valid input'); $messagetitle.val(''); $messagebox.val(''); return false; } if ($.trim($("#message").val()).length === 0) { alert('you must provide valid input'); $messagetitle.val(''); $messagebox.val(''); return false; } else { e.preventdefault(); socket.emit('send message', '<b>' + $messagetitle.val() + '</b>' + ' - ' + $messagebox.val()); $messagetitle.val(''); $messagebox.val(''); } socket.on('new message', function (data) { $chat.prepend(data + "<br/>"); }); }); });
your click function not passing in event "e" stop working when make call "e.preventdefault()".
i made jsfiddle - no socket support - alert message. http://jsfiddle.net/4v2qt/
this change (the e passing function)
$messageform.click(function (e) { ... code goes here! }
Comments
Post a Comment