-2

i dont understand why the click function is not being called?Is there any mistake in jquery?

<div id="chatbox">

</div>

        <input name="usermsg" type="text" id="usermsg" size="63" />
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
        </div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
    //If user submits the form
    $("#submitmsg").click(function(){

        var clientmsg = $("#usermsg").val();

        jQuery.ajax({method:'post',url:'{{=URL('post')}}',
                     data:{'text':clientmsg,'touser'=touser},

                    success:function(data){
            $("#usermsg").attr("value", "");
            return false;
        }})};

</script>
radha
  • 47
  • 1
  • 6

2 Answers2

0
$("form").submit(function (e) {
      e.preventDefault();
})

Check that for details: Stop form from submitting , Using Jquery

Community
  • 1
  • 1
Vladimir G.
  • 885
  • 7
  • 15
0

First you need to change your button type to button so that the form is not submitted directly when clicked. Then you need to stop the default function of the form by using event.preventDefault() method of jquery.I am only trying to resolve the problem of page refresh here.

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

 <script type="text/javascript">

//If user submits the form
$("#myForm").submit(function(e){
    e.preventDefault();



    jQuery.ajax({method:'post',url:'{{=URL('post')}}',
                 data:$('#myForm').serialize(),//its the jquery method to serialize form data directly.
       success:function(data){
        $("#usermsg").attr("value", "");

    }})};