jQuery:如何使用函数的返回值到另一个函数

时间:2013-10-16 23:10:18

标签: javascript jquery function

我有这个jquery脚本。我想将函数stockArt()的返回值用于函数prod_actname()。看来我错过了什么或者我的实现是错误的。我怎么可能那样做?

另一种情况是,当更改StockArt()值时,还必须更新活动名称。

说股票艺术颜色是绿色。并且活动名称颜色也会更新为绿色,而不从prod_actname()功能中选择绿色。当StockArt颜色也发生变化时,它会自动更改。它们必须是相同的颜色。

jQuery( document ).ready(function($) {
$('#preview_design').css("background-color","black");
$('#stock_art').change(function() {     
    var retVal = stockArt();
    $('#prod_actname').data("stockData", retVal);     
});

$('#prod_actname').change(function() {      
   var stockData = $('#prod_actname').data("stockData");
    prod_actname(stockData);
}); 


function stockArt()
{

    var returnValue = null;
    var selectStock_Art = $('#stock_art :selected').val();

        if(selectStock_Art == '31145-RB-emb')       
            { // green
                $('#stockArt_img').removeClass().addClass('stock_art_grbg1');           
            }
        else if(selectStock_Art == '33441-RB-emb1') 
            { // yellow
                $('#stockArt_img').removeClass().addClass('stock_art_yebg2');   
            }
        else if(selectStock_Art == '33441-RB-emb2') 
            { // gold
                $('#stockArt_img').removeClass().addClass('stock_art_gobg3');
            }
        else {
                $('#stockArt_img').removeClass();
            }

    returnValue = selectStock_Art;

return returnValue;

}


function prod_actname(stockData){

var StockArtResult = stockData;
    //User stockData as needed
    // assign the value to a variable, so you can test to see if it is working
    var selectActivity_Name = $('#prod_actname :selected').val();
    //alert(selectActivity_Name);

    if(selectActivity_Name == 'Baseball')
        {   
            $('#activityName_img').removeClass();

            if(stockArtValue == '31145-RB-emb'){

                $('#activityName_img').addClass('activity_name_GRbg1');
            }
            else if(stockArtValue == '33441-RB-emb1'){
                $('#activityName_img').addClass('activity_name_YEbg1');

            }else if(stockArtValue == '33441-RB-emb2') {
                $('#activityName_img').addClass('activity_name_GObg1');

            }

        }
    else if(selectActivity_Name == 'Basketball')
        {
            $('#activityName_img').removeClass();

            if(stockArtValue == '31145-RB-emb'){

                $('#activityName_img').addClass('activity_name_GRbg2');
            }
            else if(stockArtValue == '33441-RB-emb1'){
                $('#activityName_img').addClass('activity_name_YEbg2');

            }else if(stockArtValue == '33441-RB-emb2') {
                $('#activityName_img').addClass('activity_name_GObg2');

            }

        }
    else if(selectActivity_Name == 'Boys Basketball')
        {
            $('#activityName_img').removeClass();

            if(stockArtValue == '31145-RB-emb'){

                $('#activityName_img').addClass('activity_name_GRbg3');
            }
            else if(stockArtValue == '33441-RB-emb1'){
                $('#activityName_img').addClass('activity_name_YEbg3');

            }else if(stockArtValue == '33441-RB-emb2') {
                $('#activityName_img').addClass('activity_name_GObg3');
            }

        }
    else if(selectActivity_Name == 'Girls Basketball')
    {
        $('#activityName_img').removeClass();

        if(selectStock_Art == '31145-RB-emb'){

            $('#activityName_img').addClass('activity_name_GRbg4');
        }
        else if(selectStock_Art == '33441-RB-emb1'){
            $('#activityName_img').addClass('activity_name_YEbg4');

        }else if(selectStock_Art == '33441-RB-emb2') {
            $('#activityName_img').addClass('activity_name_GObg4');
        }

    }   

}

});

3 个答案:

答案 0 :(得分:2)

使用data属性存储'#prod_actname'的数据,然后再访问它。 您可以使用jQuery.data来实现此目的。 试试这个

jQuery( document ).ready(function($) {
    $('#preview_design').css("background-color","black");
    $('#stock_art').change(function() {     
        var retVal = stockArt();
        $('#prod_actname').data("stockData", retVal);     
    });

    $('#prod_actname').change(function() {      
       var stockData = $('#prod_actname').data("stockData");
        prod_actname(stockData);
    }); 


    function stockArt()
    {

        var returnValue = null;
        var selectStock_Art = $('#stock_art :selected').val();

            if(selectStock_Art == '31145-RB-emb')       
                { // green
                    $('#stockArt_img').removeClass().addClass('stock_art_grbg1');           
                }
            else if(selectStock_Art == '33441-RB-emb1') 
                { // yellow
                    $('#stockArt_img').removeClass().addClass('stock_art_yebg2');   
                }
            else if(selectStock_Art == '33441-RB-emb2') 
                { // gold
                    $('#stockArt_img').removeClass().addClass('stock_art_gobg3');
                }
            else {
                    $('#stockArt_img').removeClass();
                }

        returnValue = selectStock_Art;

    return returnValue;

    }

    var stockArtValue = stockArt();

    function prod_actname(stockData){
        //User stockData as needed
        // assign the value to a variable, so you can test to see if it is working
        var selectActivity_Name = $('#prod_actname :selected').val();
        //alert(selectActivity_Name);

        if(selectActivity_Name == 'Baseball')
            {   
                $('#activityName_img').removeClass();

                if(stockArtValue == '31145-RB-emb'){

                    $('#activityName_img').addClass('activity_name_GRbg1');
                }
                else if(stockArtValue == '33441-RB-emb1'){
                    $('#activityName_img').addClass('activity_name_YEbg1');

                }else if(stockArtValue == '33441-RB-emb2') {
                    $('#activityName_img').addClass('activity_name_GObg1');

                }

            }
        else if(selectActivity_Name == 'Basketball')
            {
                $('#activityName_img').removeClass();

                if(stockArtValue == '31145-RB-emb'){

                    $('#activityName_img').addClass('activity_name_GRbg2');
                }
                else if(stockArtValue == '33441-RB-emb1'){
                    $('#activityName_img').addClass('activity_name_YEbg2');

                }else if(stockArtValue == '33441-RB-emb2') {
                    $('#activityName_img').addClass('activity_name_GObg2');

                }

            }
        else if(selectActivity_Name == 'Boys Basketball')
            {
                $('#activityName_img').removeClass();

                if(stockArtValue == '31145-RB-emb'){

                    $('#activityName_img').addClass('activity_name_GRbg3');
                }
                else if(stockArtValue == '33441-RB-emb1'){
                    $('#activityName_img').addClass('activity_name_YEbg3');

                }else if(stockArtValue == '33441-RB-emb2') {
                    $('#activityName_img').addClass('activity_name_GObg3');
                }

            }
        else if(selectActivity_Name == 'Girls Basketball')
        {
            $('#activityName_img').removeClass();

            if(selectStock_Art == '31145-RB-emb'){

                $('#activityName_img').addClass('activity_name_GRbg4');
            }
            else if(selectStock_Art == '33441-RB-emb1'){
                $('#activityName_img').addClass('activity_name_YEbg4');

            }else if(selectStock_Art == '33441-RB-emb2') {
                $('#activityName_img').addClass('activity_name_GObg4');
            }

        }   

    }

});

答案 1 :(得分:0)

一种方法是将stockArt()的输出存储在变量中,然后将其作为参数传递给prod_actname()。为此,您需要修改prod_actname()函数的函数签名以接受参数。像这样的东西

var stockArtValue;
$('#stock_art').change(function() {     
    stockArtValue = stockArt();     
});

$('#prod_actname').change(function() {      
    prod_actname(stockArtValue);
}); 

function prod_actname(val){

 ....

}

对于某些情况,考虑到stockArtValue变量确实已被更改可能会有用。您通常可以使用另一个状态变量来指示。或者,如果您知道可能的输入值集合,则使用该输入值集之外的值对其进行初始化并检查它。

答案 2 :(得分:0)

您可以将其值保存在属性中......

jQuery( document ).ready(function($) {

var StockArtResult = null;

$('#stock_art').change(function() {     

    StockArtResult = stockArt();     
});

$('#prod_actname').change(function() {      

   if(StockArtResult != null)
   {        
       prod_actname(StockArtResult);
   }
});

});