byCoder
byCoder

Reputation: 9184

Why ajax success is called once?

Why, if i write html method in javascript, it's called only once, but if i have only alert, it's calles every time, i change wy value in input (blur).

$(".quantity").blur(function() {
    console.log("upd");
    $.ajax({ 
      url: "/line_items/update_quantity/"+$(this).attr("id"), 
      type: "GET", 
      data: {quantity: $(this).val()},
      success: function(text)
      {
        alert(text);
        $('.right').html(text);
      },
      error: function(){
        alert('Ошибка javascript');
      },
      dataType : "html"
    });
  });

I need reload html partial after every blur...

Upvotes: 0

Views: 142

Answers (2)

The System Restart
The System Restart

Reputation: 2881

If .quantity is dynamic element (I think so) then try

$(document).delegate('.quantity', 'blur', function() {
   // code
});

read here about delegate()

Upvotes: 1

Shane Courtrille
Shane Courtrille

Reputation: 14097

Try doing this..

$(document).on('blur', '.quantity', function() {
   // place your code here
});

I suspect you're replacing the dom element that the original blur binding is applied against. If you do that you remove the event handler. On() will keep it alive.

Upvotes: 2

Related Questions