本文介绍了无法通过$ .get()/$ .load()函数使用Jquery选择ID.请帮我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java语言类似

$('button').click(function(){
  //load the data and place inside to #content
});

$('#id-from-data-that-load').click(function(){
  //Do some action
});

所以这是html标记

<button>Load</button>
<div id="content">
  //Empty Data
</div>

当按钮LOAD单击html时,将是这样

when button LOAD clicked html will be like this

<button>Load</button>
<div id="content">
  <div id="id-from-data-that-load">
    content that load
  </div>
</div>

但是,当我单击div id-from-data-load时,该功能将无法运行.

BUT, when i clicked div id-from-data-load the function won't run.

如何解决?

推荐答案

您需要将live用于div事件.应该是这样的:

You need to use live for the div event instead. Here's what it should be:

$('#id-from-data-that-load').live("click", function(){
  //Do some action
});

或者,您也可以这样做:

Or alternatively you could also do this:

var submitted = false;

$('button').click(function(){
  // If the button was clicked before, we don't submit again.
  if (submitted == true) { return false; }

  // Set submitted to true, so when user clicks the button again,
  // this operation will not be processed one more time.
  submitted = true;

  //load the data and place inside to #content
  $.post("/getdata", {}, function(response) {
      //after the load happened we will insert the data into the div.
      $("#content").html(response);

      // Do the bindings here.
      $('#id-from-data-that-load').click(function(){
        //Do some action
      });
  });

  return false;
});

这篇关于无法通过$ .get()/$ .load()函数使用Jquery选择ID.请帮我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:02