1

I am facing a problem in ajax jquery on success. My whole jsp reload on ajax success even after putting return false. How to prevent page reloading.

function AjaxCall(category){

var formValue = $('#TP').serialize();
$.post('saveIndvQuestionnaire.jsp?'+formValue+'&category='+category,
//$.post('callPage.jsp',
        function(data) {
    return false;
});
}

I am calling AjaxCall function from input type image

<input id="test" type="image" onclick="javascript:AjaxCall('Test');" src="../images/v10/larrow.png">

I am not sure I read somewhere type images makes two calls onclick.

Thanks.

pise
  • 849
  • 6
  • 24
  • 51

3 Answers3

1

Perhaps the problem is the behavior of input type image. https://developer.mozilla.org/es/docs/Web/HTML/Element/Input Try to use e.preventDefault() in your function and no Submit should be done.

$( "#test" ).click(function( event ) {
  event.preventDefault();
var formValue = $('#TP').serialize();
$.post('saveIndvQuestionnaire.jsp?'+formValue+'&category='+category,
        function(data) {
    return false;
});

});

Exmple

http://jsfiddle.net/HBeVv/10/

You can use another html control to achieve this like a href with a Image:

<a href="#" onClick="alert('Hello World!')"><img title="The Link" /></a>
Jaime García Pérez
  • 953
  • 1
  • 10
  • 16
1

I think you can also Like this

$('YourForm').submit(function(e){
   e.preventDefault();
   formdata=$(this).serialize();
   $.ajax({
      type: "POST",
      url: url,
      data: formdata
      }).done(function(data){
  //do something
   });
});
1

Try this:

<input id="test" type="image" onclick="return javascript:AjaxCall('Test');" src="../images/v10/larrow.png">

You should get "return" the response onclick.

Naveen Chandra Tiwari
  • 5,055
  • 3
  • 20
  • 26