Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js隐藏函数_Javascript_Jquery - Fatal编程技术网

Javascript js隐藏函数

Javascript js隐藏函数,javascript,jquery,Javascript,Jquery,加载页面时(第一个下拉列表(div StatusID)是从mysql数据库动态填充的),或者用户从第一个下拉框中选择undeposed-EI时,div status_sub_6显示第二个select语句 My.hide.show函数在更改时可以正常激活,但即使第一个select(StatusID)的动态填充值满足比较条件,该函数也会在加载页面时隐藏第二个下拉列表 我确信我需要一个onload函数来覆盖下面的.js代码,但是我非常希望在编写额外代码时能得到一些帮助 JavaScript: $(do

加载页面时(第一个下拉列表(div StatusID)是从mysql数据库动态填充的),或者用户从第一个下拉框中选择undeposed-EI时,div status_sub_6显示第二个select语句

My.hide.show函数在更改时可以正常激活,但即使第一个select(StatusID)的动态填充值满足比较条件,该函数也会在加载页面时隐藏第二个下拉列表

我确信我需要一个onload函数来覆盖下面的.js代码,但是我非常希望在编写额外代码时能得到一些帮助

JavaScript:

$(document).ready(function(){
    $('#status_sub_6').hide();

     $('#StatusID').change(function () {
        if ($('#StatusID option:selected').text() == "Unemployed - EI"){
            $('#status_sub_6').show();
        }
         else { 
              $('#status_sub_6').hide();
         }
    });
});
尝试:

您可以通过在加载后立即触发更改事件来实现这一点

$(document).ready(function(){
    ...
    // your current code
    ...
    $('#StatusID').trigger('change'); // trigger a change
});

您也可以这样做:

$(document).ready(function(){
    if ($('#StatusID option:selected').text() == "Unemployed - EI"){
        $('#status_sub_6').hide();
    }

     $('#StatusID').change(function () {
        if ($('#StatusID option:selected').text() == "Unemployed - EI"){
            $('#status_sub_6').show();
        }
         else { 
              $('#status_sub_6').hide();
         }
    });
});
或者更优雅的解决方案:

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

    $('#StatusID').change(function () {
        toggleSub();
    });
});

function toggleSub() {
    if ($('#StatusID option:selected').text() == "Unemployed - EI") {
        $('#status_sub_6').hide();
    }
    else {
        $('#status_sub_6').show();
    }
}

仅在加载条件时触发met@Huangism-标准正在更改处理程序中进行检查。我选择这一个是因为它对我来说似乎是最符合逻辑的,并且完全符合我的需要。你们太棒了。我希望将来能为别人的问题做出贡献,就像很多人帮助过我一样。谢谢
$(document).ready(function () {
    toggleSub();

    $('#StatusID').change(function () {
        toggleSub();
    });
});

function toggleSub() {
    if ($('#StatusID option:selected').text() == "Unemployed - EI") {
        $('#status_sub_6').hide();
    }
    else {
        $('#status_sub_6').show();
    }
}