English 中文(简体)
J Query: can t re-update links (Im new to JQuery)
原标题:JQuery: can t re-update links (I m new to JQuery)

I m new to Jquery and I m getting crazy to reupdate in ajax a code containing urls!! It works the first time, but the link does not work any longer after ajax response rewrites html.

请参看我的法典。

First the HTML markup (View)

<head>  
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">  
  $(document).ready(function() { 
    $("a[class=unselected_quid], a[class=quid_selected]").click(function(){
        var url = $(this).attr("name");
        alert(url);
        $.ajax({
            url: "index.php", 
            type: "POST",  
            data: url,
            dataType: "html",
            success: function(msg) {    
                $("#result").html(msg);  
            },
            error: function(){
                alert("error!!!");
            } 
        });
    });
  });
</script>  
</head> 

<?php // set url strings in php
  $url = "component=Training&ctrl=JQueryTraining&task=ajaxLinkResponse&data=1";
  $url2 = "component=Training&ctrl=JQueryTraining&task=ajaxLinkResponse&data=2";
?>

<body>  
  <div id="result"><br>
    <a name= <?echo $url?>  href= javascript:void(0)  title= uno  class= selected_quid >link a</a> <br>
    <a name= <?echo $url2?>  href= javascript:void(0)  title= due  class= unselected_quid >link b</a><br>
  </div>  
</body>

然后是PHP(主计长)

public function ajaxLinkResponse(){
  $url = "component=Training&ctrl=JQueryTraining&task=ajaxLinkResponse&data=1";
  $url2 = "component=Training&ctrl=JQueryTraining&task=ajaxLinkResponse&data=2";

  $code = "<a name= {<?$url?>}  href= javascript:void(0)  title= uno  class= selected_quid >link a</a> 
  <a name= {<?$url2?>}  href= javascript:void(0)  title= due  class= unselected_quid >link b</a>";  

  echo($code);
}
最佳回答

You need to use .live().delegate() instead of .click() as you are making new DOM elements in your AJAX call and .click() does not cater f或 DOM element creation.
i.e.

$("a[class=unselected_quid], a[class=quid_selected]").live( click ,function(e){
e.preventDefault();
// rest of code

$(aWrapper).delegate( a[class=unselected_quid], a[class=quid_selected] , click ,function(e){
e.preventDefault();
// rest of code

You may ask What s the difference? well this should help. Basically delegate is faster, will not crash with heavy DOM traversals, and you can supply a context (this is a big feature).

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签