1

我有一个像这样的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

网页只是显示为空白,我不知道会发生什么。如果这是不可能的,还有其他方法吗?

4

1 回答 1

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 })
于 2013-07-22T15:18:22.727 回答