1

In one of my search bars, upon keyup a div is loaded with the contents of another file.

<input type='text' value='search' id='search' />

My JS code for this task is:

$("#search").keyup(function(){  
        var search = $(this).val(); 
        var url = "model/quickpicks_backend.php"; 
        var league = $('#league_form').val();
        var position = $('#compose').val();
        var data = "search="+ search +"&league="+ league +"&position="+ position;
        $('#load').load(url, data);
    });

All of the URL parameters are effectively passed to model/quickpicks_backend.php and based upon the values of the three parameters that are passed along, PHP will echo out anywhere from 0 to 5 results. Whatever is echoed out will be wrapped inside of a with the class of "result." There is also some JS code. It is as follows:

$('.sky').click(function(){
        var num = $('#num').text();
        var num_trim = num.trim();
        var new_num = Number(num) + Number(1);
        $('#num').text(new_num);
        var id = $(this).attr('id');
        var player_id = Number(id) - Number(5);
        var league = $(this).attr('title');

        $.ajax({  
            type: 'GET',  
            url: 'model/quickpicks_list.php',  
            data: 'num='+ num +'&player_id='+ player_id +'&league='+ league,
            dataType: 'JSON',
            success: function(data)
            {  
                $.parseJSON(data);

                $('#'+ id).fadeOut('fast');
                $('#search').val('');
                $('#num').text(new_num);    
                $('#'+ num_trim).html(data.value);

                if(num == 5)
                {
                      alert("done");
                }
            }
        });
    });

The problem is that whenever .sky is clicked, the data from the most recent element that was clicked isn't collected. Rather, it's all the same info from the very first time a h1.sky is clicked. I thought that maybe using

$('.sky').each(function(index) { 
     $(this).click(function(){

    });
});

instead would work. But, it didn't. Using

$('.sky').live();

sort of worked but upon the 5th click (the click that is supposed to trigger the alert), it alerts multiple times.

A working example can be found here

4

2 回答 2

0

the syntax is

$('.sky').live("click",function(
   ........................
));

this might be the problem

于 2013-02-26T11:02:30.827 回答
0

Try using a break statement just after the alert statement so that it will move out of the loop.

于 2013-02-26T11:03:11.657 回答