Jquery ajax loop problem

I’d like make this effection.

Firstly, there have some tag links in my main page. click each one, post value to 2.php with jquery.ajax and turn back value in div#result.

2.php have a search box. when search something in it. the result data will still show in the div#result.

my problem is: I know if I will do jqeury ajax in the b.php, I shall write the jqeury code in the first success part. but this only can control one time, when I continue search in the search box, the jquery not work. I think I met a loop problem. How to solve it?

1.php

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">    
 $(document).ready(function(){
    $('.click').click(function(){
    var value1 = $(this).text();
        $.ajax({
            url: "2.php", 
            dataType: "html",
            type: 'POST', 
            data: "data=" + value1, 
            success: function(data){
            $("#result").html(data);
            $('#search').click(function(){
            var value = $('#search1').val();
                $.ajax({
                    url: "2.php", 
                    dataType: "html",
                    type: 'POST', 
                    data: "data=" + value, 
                    success: function(data){
                        $("#result").html(data);
                    }
                });
            });
            }
        });
      });
    });
</script>
<a rel="aa" class="click">aa</a>
<a rel="aa" class="click">bb</a>
<div id="result"></div>

2.php

<?php
echo $_POST['data'];
?>
<form name="form">
<input type="text" value="" id="search1">
<a name="nfSearch" id="search">search</a>
</form>

When a new element is introduced to the page the jQuery .click() method becomes useless because it can only see elements that were part of the original DOM. What you need to use instead is the jQuery .live() method which allows you to bind events to elements that were created after the DOM was loaded. You can read more about how to use it at the below link.

.live() – jQuery API

More or less a simple example below should help.

$('#search').live('click', function(e) {
    // Prevent the default action
    e.preventDefault();
    
    // Your code here....
});

@SgtLegend, great! This works well.

I also asked in

http://stackoverflow.com/questions/6252799/search-box-in-a-jquery-ajax-success-page-problem-in-loop

If u have an account in there, and paster your answer, I will give u the accepted mark. Thanks again :slight_smile:

Your welcome and i posted the answer on Stack overflow so users can reference it.