简体   繁体   English

与keyup结合使用时,jQuery Live功能不起作用

[英]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. 我使用loadData()函数从我的php脚本中获取数据,并且使用另一个函数从数据库中搜索。 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, 这是我正在使用的jQuery函数,

//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(). 不推荐使用.live(),因此您可以尝试使用.on()代替.live()。

http://api.jquery.com/live/ & http://api.jquery.com/on/ 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? 您正在使用哪个jQuery版本? the live function may be deprecated. 实时功能可能已被弃用。 Use .on() instead or .click() directly? 改用.on()还是直接使用.click()?

Use On () as live () is deprecated. 不建议将live ()用作On ()。

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

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

I have replaced live with on but it doesn't work for me. 我已将live替换为on,但对我不起作用。 You can use .delegate() 您可以使用.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. 当我更新JQuery库时,实时程序停止工作,并立即用委托将其替换。

See more information here: https://api.jquery.com/delegate/ 在此处查看更多信息: https : //api.jquery.com/delegate/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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