开发者

How to prevent form submission and make jquery ajax call instead

I have an event to capture when the return key is pressed o开发者_如何学Gon a form input field, but the ajax call is not fired, instead the from is submitted as a normal http request.

$("#addurl").keypress(function(e){
        switch(e.keyCode){
            case 13:
                // add url ajax call
                $("body").html(ajax_load)
                .load(loadUrl,{url:$("#addurl").value},function(responseText){
                    $("#videos").append(responseText);
                }
                return false;
        }
    });

...

<form><input id="addurl" name="addurl"/></form>

The case 13 statement was fired correctly when I tested with alert("hi") so why is the ajax call not taking the place of a normal full-blown http request?


you can just use

$(document).ready(function(){
//capture the form submission event
$("#formID").submit(function(){
   //do something
   return false;  
});

});


$("#inputBoxId").keypress(function(e){            
        if(e.keyCode==13)
         {
            e.preventDefault();  /// add prevent default
            //your code here

         }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜