简体   繁体   中英

Jquery Live function is not working when used with keyup

I use loadData() function to get data from my php script and I use another function to search from database. All works fine, but when I click on the search result, it's not loading the data. Here is the jquery functions I am using,

//Gets Data
function loadData(id)
{
loading_show();
$.ajax({
      url: "load_data.php",
      type: "post",
      data: "id="+id,
      success: function(data){
            loading_hide();
           $("#container_wrap_metro").html(data);
      },
      error:function(){
          alert("failure");
          $("#container_wrap_metro").html('Unable to process request!');
      }  
    }); 
}


//Search
$(function(){
  $("#result").keyup(function(){
    var q = $(this).val();
    $.get("results.php?q="+q, function(data){
    if(q){
        $(".side_container").html(data);
    } 
    else {
        $(".side_container").html("");
    }
  });
});
$('#b').live('click',function(){
  var page = $(this).attr('p');
  loadData(page);
  }); 
});

.live() is Deprecated so you can try .on() instead of .live().

http://api.jquery.com/live/ & http://api.jquery.com/on/

The answer is:

$(document).on('click','#b',function(){...}); //live equivalent

But better is to use closest static container:

$('#container_wrap_metro').on('click','#b',function(){...});

Which jQuery version are you using? the live function may be deprecated. Use .on() instead or .click() directly?

Use On () as live () is deprecated.

$(document).on("click", "a.foo", fn);  //here a.foo is target

http://jquery.com/upgrade-guide/1.9/#live-removed

I have replaced live with on but it doesn't work for me. You can use .delegate()

In my case, I have written a click in the dynamically loaded content. When I updated the JQuery Library, the live stopped working & replaced it with delegate today.

See more information here: https://api.jquery.com/delegate/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM