开发者

Same JQuery click function for numerous links

开发者 https://www.devze.com 2022-12-22 07:58 出处:网络
I have a page that has multiple links with various attributes (these attributes will be pulled in 开发者_Python百科from a database):

I have a page that has multiple links with various attributes (these attributes will be pulled in 开发者_Python百科from a database):

index.php

<html>
<head>
<script type='text/javascript' src='header.js'></script>
</head>
<body>
<a href="#" class="link_click"id="12">My_Link_1</a>
<a href="#" class="link_click"id="21">My_Link_2</a>

<div id='my_container'> </div>

</body>
</html>

My header.js file has:

$(document).ready(function(){
    $('.link_click').click(function(){
      $("#my_container").load("classes/class.project.php", {proj: $(this).attr('id')} );
      return false;
  });
});

class.project.php is pretty simple:

<?php
echo "<div id='project_container'>project = ".$_POST['proj']." : end project</div>";
?>

This loads and passes the ID variable (which actually comes from a database) to class.project.php. It works fine for the first link click (either link will work). Once one link is clicked no other links with this div class will work. It feels like javascript loads the class.porject.php and it will not refresh it into that #my_container div.

I tried running this as suggested by peterpeiguo on the JQuery Fourm, with the alert box for testing wrapped inside .each:

Copy code

$(document).ready(function() {
     $('.link_click').each(function() {
        $(this).click(function() {
          alert($(this).html());
     });
  });
});

This seems to work fine for the alert box. But when applying it to .load() it does not reload the page with the new passed variable. As a matter of fact, it doesn't even reload the current page. The link performs no function at that point.

The example site can be viewed here: http://nobletech.net/gl/


I looked at the link you posted, and the problem is that when you're doing load you're replacing the elements on the page with new ones, thus the event handlers don't work anymore.

What you really want to do is target the load. Something like:

$("#project_container").load("classes/class.project.php #project_container", {proj: $(this).attr('projid')} );

This only loads stuff into the proper container, leaving the links and other stuff intact.

Ideally, the php script should only return the stuff you need, not the whole page's markup.

BTW- Caching shouldn't be an issue in this case, since .load uses POST if parameters are passed. You only have to worry about ajax caching with GETs


Sounds like the request is getting cached to me.

Try this:

 $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
});


Sorry but this might be completely wrong but after examining your XHR response I saw that you are sending back html that replaces your existing elements.

So a quick fix would be to also send the following in your XHR response (your php script should output this also):

<script>
$('.link_click').each(function() {
    $(this).click(function() {
      alert($(this).html());
 });
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消