2013-07-22 21 views
1

我有一個AJAX功能是這樣的:問題創造功能時,裏面的AJAX

for (var i = 0; i < 50; i++) (function(i) { 
    jQuery('#skin_'+i).click(function() { 
     var result = $('#idskin_'+i).val();    
     $.ajax({ 
      url: 'ajax.php', 
      type:'POST', 
      dataType : 'json', 
      data: { 'dataString': result }, 
      beforeSend: function(){ 
       $("#loader").show(); 
      }, 
      success: function(output_string){ 

           $("#1").css({ 
       background: "url("+output_string['textu']+")", 
       border: "1px solid "+output_string['color'] 
       });     
       $("#2").css({ 
       background: "url("+output_string['textu']+")", 
       border: "1px solid "+output_string['color'] 
       });     
       $("#3").css({ 
       background: "url("+output_string['textu']+")", 
       border: "1px solid "+output_string['color'] 
       }); 

           } // End of success function of ajax form 
     });   
     }); 
    })(i); //for 
}); 

一切工作正常,直到我試圖創建這樣這個AJAX功能內的功能(我將指向剛剛成功塊的代碼):

  success: function(output_string){ 

      jQuery.exists = function exists(){ 

      $("#1").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
      });     
      $("#2").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
      });     
      $("#3").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
      }); 

       }); //end of custom function 

          } // End of success function of ajax form 

該網頁只顯示爲空白,我不知道可能發生了什麼。如果這不可能,那麼還有其他辦法嗎?

+3

'jQuery.exists =函數(){'你不命名一個匿名/閉合功能。 –

+0

沒有像AJAX這樣的功能。這是一個常規的回調函數,使用你的瀏覽器調試器來找出發生了什麼。 – Andrey

回答

2

你有2個語法錯誤..代碼更改爲:

jQuery.exists = function(){ // don't give it the name again 

    $("#1").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
    });     
    $("#2").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
    });     
    $("#3").css({ 
      background: "url("+output_string['textu']+")", 
      border: "1px solid "+output_string['color'] 
    }); 

} // you only need curly here not }) 
+0

是的好友我有一個語法錯誤,我剛剛修復。感謝您的支持。 –

+0

@HerlandCid樂於幫助:) –