简体   繁体   English

年份间隔的 Javascript 下拉菜单

[英]Javascript drop down menus for year intervals

I have two drop down menus that are used to set up year intervals for an SQL query.我有两个下拉菜单,用于为 SQL 查询设置年份间隔。 These drop down menus are populated using an SQL query as well.这些下拉菜单也使用 SQL 查询填充。 I have them both working, but I would like to improve on my design.我让他们都工作,但我想改进我的设计。 When I select a year from the first drop down menu, the second menu is populated with year values >= to the first box (If I select 1991, the second menu will contain 1991, 1992, ... etc. However, when I select a new value from the first drop down box, the second one resets. I would like to have it where if I go back to the first drop down box and select a year that is less than the one selected in the second box, the values won't reset. Is there a way I can do that?当我从第一个下拉菜单中选择年份时,第二个菜单填充了年份值 >= 到第一个框(如果我选择 1991,第二个菜单将包含 1991、1992 等。但是,当我从第一个下拉框中选择一个新值,第二个将重置。如果我返回第一个下拉框并选择比第二个框中选择的年份少的年份,我希望将它放在那里值不会重置。有没有办法做到这一点?

$(document).ready(function () {
$("#first").change(function () {
    var val = $("#first option:selected").html();
    $("#second").html("");
    var d = new Date();
    var n = d.getFullYear();

    for (i = val; i <= n; i++) {
        $("#second").append("<option>" + i + "</option>");
    }
});
});
$(document).ready(function () {
    $("#first").change(function () {
        var val  = $("#first option:selected").html(),
            val2 = $("#second").val(),
            d    = new Date(),
            n    = d.getFullYear(),
            y    = [];

        for (i = val; i <= n; i++) {
            y.push( $("<option />", {text: i, value:i}) );
        }

        $("#second").html(y).val(function() {
            return $('option[value="'+val2+'"]', this).length ? val2 : '';
        });
    });
});

FIDDLE小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM