javascript
  • jquery
  • debugging
  • 2011-07-13 156 views -2 likes 
    -2

    这段代码有什么问题?我正在使用jQuery。这段代码有什么问题?

    function setPreviewUrl() { 
         $post_title = $("#post_title").val(); 
         $post_content = $("#post_content").val(); 
         $.get('ajax/base64encode.php?name='+post_title, function(data1) { 
          var $postTitle = null; 
          $postTitle = data1; 
         }); 
         $.get('ajax/base64encode.php?name='+post_content, function(data2) { 
          var $postCont = null; 
          $postCont = data2; 
         }); 
         var $urlExten = null; 
         $urlExten = '?post_title='+postTitle+'&post_cont='+postCont; 
         $("a[href*='#change']").attr('href', '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
         $("#preview_frame").prop("href", '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
        } 
    
    +0

    你期望它做,当你运行它时会发生什么? – interjay

    +0

    这是什么问题? – rahul

    +0

    也许你应该写下你想要达到的要求 –

    回答

    1

    好像你搞乱了异步代码:

    function setPreviewUrl() { 
         var $post_title = null; 
         $post_title = $("#post_title").val(); 
         var $post_content = null; 
         $post_content = $("#post_content").val(); 
         $.get('ajax/base64encode.php?name='+post_title, function(data1) { 
          var $postTitle = null; 
          $postTitle = data1; 
         }); 
         $.get('ajax/base64encode.php?name='+post_content, function(data2) { 
          var $postCont = null; 
          $postCont = data2; 
         }); 
         // Those functions above will execute when the get is done, in the 
         // meantime the code below is executed, so the variables have not been 
         // set yet. 
         // Also, the variables are only declared inside the get function so they 
         // are not accessible due to their scope. 
         var $urlExten = null; 
         $urlExten = '?post_title='+postTitle+'&post_cont='+postCont; 
         $("a[href*='#change']").attr('href', '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
         $("#preview_frame").prop("href", '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
        } 
    
    相关问题