0

如何将“appName”和“rating”从初始点击函数传递到“parse”函数?预先感谢您的任何帮助。我尝试过使用data: {appName: appName, rating: rating},但如果确实有效,我仍然不确定如何在“解析”函数中捕获数据

          <script>
            $(document).ready(function(){
              $('.one, .two, .three, .four, .five').click(function(){

                    //get the app name that's to be rated.
                    var appName = $(this).parents('*:eq(4)').text();
                    appName = appName.split("\n");
                    appName = appName[2]

                    //get the rating given to the app.
                    var rating = $(this).attr('class');

                    if(rating == 'one'){
                        rating = 1;
                    }
                    else if(rating == 'two'){
                        rating = 2;
                    }
                    else if(rating == 'three'){
                        rating = 3;
                    }
                    else if(rating == 'four'){
                        rating = 4;
                    }
                    else if(rating == 'five'){
                        rating = 5;
                    }


                $.ajax({
                    url: 'http://xxxxxx',    
                    dataType: 'xml',  
                    success: parse,
                    error: loadfail
                });

                });
            });
        </script>

        <script>
        function loadfail(){
          alert("Error: Failed to read file!");
        }

        function parse(document){

            $(document).find("ViewAll").find("ROW").each(function(){
                 var optionLabel = $(this).find('SC_DF_FIELD_1').text();
                 var optionValue = $(this).find('SC_DF_FIELD_4').text();
                 alert(optionLabel + ":" + optionValue);
            });
        }
        </script>
4

1 回答 1

3

您可以将内联函数传递给success参数,其中包含您需要的变量。

$.ajax({
    url: 'http://xxxxxx',    
    dataType: 'xml',  
    success: function(result) { parse(result, appName, rating); },
    error: loadfail
});

function parse(result, appName, rating) {
    // etc...
}
于 2013-07-10T15:40:38.480 回答