0

我有这个 jquery 脚本。我想将函数的返回值stockArt()用于 function prod_actname()。看来我遗漏了一些东西或者我的实现是错误的。我怎么可能这样做?

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

说 Stock Art 颜色是绿色。并且活动名称颜色也更新为绿色,无需从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');
        }

    }   

}

});

4

3 回答 3

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');
            }

        }   

    }

});
于 2013-10-16T23:15:20.543 回答
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 变量确实已更改可能很有用。您通常可以使用另一个状态变量来指示。或者,如果您知道一组可能的输入值,则使用该输入值集之外的值对其进行初始化并对其进行检查。

于 2013-10-16T23:15:09.017 回答
0

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

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

var StockArtResult = null;

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

    StockArtResult = stockArt();     
});

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

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

});

于 2013-10-16T23:16:14.020 回答