【问题标题】:Resetting select options based on choice根据选择重置选择选项
【发布时间】:2016-08-03 19:02:39
【问题描述】:

我有以下数据集

array:4 [
  "data" => array:2 [
    2015 => array:2 [
      "english" => array:1 [
        "chips" => array:1 [
          0 => "img1.png"
        ]
      ]
      "french" => array:1 [
        "mussles" => array:1 [
          0 => "img1.png"
        ]
      ]
    ]
    2016 => array:2 [
      "indian" => array:1 [
        "madras" => array:1 [
          0 => "img1.png"
        ]
      ]
      "italien" => array:1 [
        "pasta" => array:1 [
          0 => "img1.png"
        ]
      ]
    ]
  ]
]

为了正确显示正确的数据,我执行以下操作

<select id="year" class="form-control">
    @foreach($fileData["data"] as $year => $countries)
        <option value="{{ $year }}">{{ $year }}</option>
    @endforeach
</select>

<select id="country" class="form-control hide">
    @foreach($fileData["data"] as $year => $countries)
        @foreach ($countries as $country => $dishes)
            <option class="year-{{ $year }} hide" value="{{ $country }}">{{ $country }}</option>
        @endforeach
    @endforeach
</select>

<select id="dish" class="form-control hide">
    @foreach($fileData["data"] as $year => $countries)
        @foreach ($countries as $country => $dishes)
            @foreach ($dishes as $dish => $images)
                <option class="year-{{ $year }} country-{{ $country }} hide" value="{{ $dish }}">{{ $dish }}</option>
            @endforeach
        @endforeach
    @endforeach
</select>

所以第二个和第三个选择被隐藏,直到前一个被选中。
在 JavaScript 中我会这样做

var getSelectedYear = function() {
    return $("#year option:selected").val();
}

var getSelectedCountry = function() {
    return $("#country option:selected").val();
}

$('#year').change(function() {
    $("#country option.year-" + getSelectedYear()).removeClass('hide');
    $("#country").removeClass('hide');
})

$('#country').change(function() {
    $("#dish option.year-" + getSelectedYear() + ".country-" + getSelectedCountry()).removeClass('hide');
    $("#dish").removeClass('hide');
})

所以如果我在第一个选择中选择 2015,第二个选择将显示英语和法语。如果我然后选择英语,第三个选择将显示筹码。

这一切都很好。我的问题是这个。如果我选择 2015 年,英语和薯条,然后将英语更改为法语,则第三个选择选项现在显示薯条和 mussles 而不仅仅是 mussles。所以它工作正常,它按顺序完成,但是一旦你改变了一些东西,我需要在动态填充它们之前重置以下选择选项。

有什么办法可以做到吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    在取消隐藏您想要的选项之前隐藏所有其他选项怎么样?

    $('#year').change(function() {
        // hides all the options
        $("#county option").addClass("hide");
        $("#country option.year-" + getSelectedYear()).removeClass('hide');
        $("#country").removeClass('hide');
    
        // gets the value of the first available option and resets your dropdown to that
        var first_val = $("#country option:not('.hide')").first().val();
        $("#country").val(first_val);
        // manually trigger the change so that it propagates to the following dropdown
        $("#country").trigger("change");
    })
    
    $('#country').change(function() {
        // hides all the options
        $("#dish option").addClass("hide");
        $("#dish option.year-" + getSelectedYear() + ".country-" + getSelectedCountry()).removeClass('hide');
        $("#dish").removeClass('hide');
    
        // gets the value of the first available option and resets your dropdown to that
        var first_val = $("#dish option:not('.hide')").first().val();
        $("#dish").val(first_val);
    })
    

    这会将“隐藏”类添加到所有可用选项中,然后仅取消隐藏您希望可用的特定选项。此外,它会选择第一个可用选项,因此您不会看到过时的下拉菜单。

    【讨论】:

    • 谢谢,这很有帮助。不过有一件事我想不通。假设您选择 2015 > English,第三个选项应显示筹码。但是再说你改成2016,第二个选项变化正确,但是第三个选项还是有筹码,只有再次选择第二个选项时才会变化。如果重新选择年份,是否可以清除第三个选项,直到再次选择第二个?谢谢
    • 啊,是的。我希望设置该值会自动触发change 事件。既然没有,请手动触发它。我会把它添加到我的答案中
    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 2012-01-19
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    相关资源
    最近更新 更多