通过onchange函数jquery传递varibel

时间:2018-10-13 10:58:00

标签: javascript jquery

我有用于显示/隐藏div的脚本

var folder;
$(function() {
    $('#teamleag').change(function() {
        if ($('#teamleag').val() == 'footballal') {
            $('#selectleag').show();
            $('#selectleag1').hide();
            $('#selectleag2').hide();
            folder = "basket";
        } else if ($('#teamleag').val() == 'footballleomit') {
            $('#selectleag').hide();
            $('#selectleag1').show();
            $('#selectleag2').hide();
            folder = "footballal";
        } else if ($('#teamleag').val() == 'basketball') {
            $('#selectleag').hide();
            $('#selectleag1').hide();
            $('#selectleag2').show();
            folder = "football_leomit";
        }

    });
});

在div中,我有一个select选项标签,该标签在选中时显示图像

$(document).ready(function() {

    $("#hometeam").change(function() {
        var src = $(this).val();
        $("#imagePreview").html(src ? '<img class=home src="img/teamslogo/' + folder + '/' + src + '">' : '');
    });
});
$(document).ready(function() {

    $("#awayteam").change(function() {
        var src = $(this).val();
        $("#imagePreview1").html(src ? '<img class=home src="img/teamslogo/' + folder + '/' + src + '">' : '');
    });
});

图像来自不同的文件夹。如何传递带有文件夹名称的变量并将其链接到所选的div。我试图放入文件夹全局变量并在它们之间传递它,但是我得到了undefined

1 个答案:

答案 0 :(得分:-1)

采用如下所示的隐藏字段

<input id="folderName" type="hidden" value="">

$(function() {
    $('#teamleag').change(function() {
        if ($('#teamleag').val() == 'footballal') {
            $('#selectleag').show();
            $('#selectleag1').hide();
            $('#selectleag2').hide();
            $('#folderName').val('basket');
        } else if ($('#teamleag').val() == 'footballleomit') {
            $('#selectleag').hide();
            $('#selectleag1').show();
            $('#selectleag2').hide();
            $('#folderName').val('footballal');
        } else if ($('#teamleag').val() == 'basketball') {
            $('#selectleag').hide();
            $('#selectleag1').hide();
            $('#selectleag2').show();
            $('#folderName').val('football_leomit');
        }

    });
});
$(document).ready(function() {

    $("#awayteam").change(function() {
        var src = $(this).val();
        var folder = $("#folderName").val();
        $("#imagePreview1").html(src ? '<img class="home" src="img/teamslogo/' + folder + '/' + src + '">' : '');
    });
});

希望此代码能正常工作....