在ajax中创建函数的问题

时间:2013-07-22 15:16:09

标签: jquery 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

网页显示为空白,我不知道可能发生了什么。如果无法做到这一点,还有其他方法吗?

1 个答案:

答案 0 :(得分: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 })
相关问题