在javascript中使用来自ajax调用的var

时间:2012-08-19 14:30:43

标签: javascript ajax var

  

可能重复:
  Javascript callback functions with ajax

我从ajax成功调用中设置了一个var。此var包含数组中的值。当我在ajax调用之后直接输出var时,它包含数组中的所有值:

        $(function() {
        $("#find").click(function() {

            var url = $("#form_url").val();
            var dataString = 'url=' + url;

            $.ajax({
                type: "POST",
                url: "/ajax/add_url.php",
                data: dataString,
                }).done(function( result ) {
                    myresult(result);
            });

            return false;
        });
    });


function myresult(result) {
var result_lines = result.split("<splitter>");    
if (result_lines[0] == '1') { 
    $('#content_error').html(result_lines[1]).fadeIn(250);
    $('#content_error').delay(1500).fadeOut(500);
} else if (result_lines[0] == '2') { 
    $('#content_success').html('Succesfully received images').fadeIn(250);
    $('#url_result').delay(500).fadeIn(500);
    $('#content_success').delay(1500).fadeOut(500);
    var array = result_lines[1];
    alert(array);
}
return true;   

}

如何在成功的ajax调用之后显示的javascript中使用var数组?

我想使用var的Javascript:

 var array="Here I want to use the var array which is set in the earlier ajax call";
 var max = $(images).length;
 var imgPtr = 0;

 $('#swapnextimg').click{

    if (imgPtr == max) {
        //comment the below return and set imgPtr to 0 for rotating the images
         return;
    }

     $("#imageswap").attr('src', images[imgPtr++]);
  }

 $('#swapprevsimg').click{

    if (imgPtr == 0) {
        //comment the below return and set imgPtr to max-1 for rotating the images
        return;
    }

    $("#imageswap").attr('src', 'images[imgPtr--]);
 }

 <a id="swapnextimg"></a>
 <a id="swapprevsimg"></a>
 <div id="imagebox">
     <img id="imageswap" src="http://site.com/images/123456.jpg">
 </div>

0 个答案:

没有答案