简体   繁体   English

如何将 select 输入动态匹配到 Materialise CSS 中的选定选项?

[英]How to match select input to selected option dynamically in Materialize CSS?

I'm using Materialize CSS 1.0.0 (so no jQuery) and I have a select drop-down where users can select different themes (auto, light, dark).我正在使用 Materialize CSS 1.0.0(所以没有 jQuery),我有一个 select 下拉菜单,用户可以在其中使用 Z99938282F04071859941E18,F16EFCF42Z 不同的主题(auto light) The default is auto (it uses the user's system preference - light or dark).默认值为自动(它使用用户的系统偏好 - 亮或暗)。 Every page has this drop-down.每个页面都有这个下拉菜单。 It all works as it should except if I open the site and go to the select and select, let's say, light, and then I navigate to another page, the select input says auto. It all works as it should except if I open the site and go to the select and select, let's say, light, and then I navigate to another page, the select input says auto. However, the drop-down has light selected (in Materialize, the selected item has a grey background), and then when I close the drop-down by clicking outside of it, not clicking the light option, the input says light.但是,下拉菜单选择了灯光(在 Materialize 中,所选项目具有灰色背景),然后当我通过单击下拉菜单而不是单击灯光选项关闭下拉菜单时,输入显示灯光。

So, to reiterate, I select a theme other than the default (auto), that option is selected across the site, but the input does not match the selected option across the site UNTIL I open the drop-down where the current theme IS selected, and then click outside of it, and then the input matches what was selected (changes from auto to light, for instance).因此,重申一下,我 select 是默认(自动)以外的主题,该选项在整个站点中被选中,但输入与整个站点中的选定选项不匹配直到我打开选择当前主题的下拉菜单,然后在其外部单击,然后输入与所选内容匹配(例如,从自动更改为亮)。 It's odd because everything is working as it should except the input doesn't initially match the selected option.这很奇怪,因为除了输入最初与所选选项不匹配外,一切都在正常工作。 I think it's because there's some kind of conflict between my JS for switching the themes and Materialize's select "preset" which relies on its own JS in the materialize.js file.我认为这是因为我的切换主题的 JS 与 Materialize 的 select “预设”之间存在某种冲突,该预设依赖于 materialize.js 文件中自己的 JS。 I tested my code for switching themes using just a regular HTML select, and it worked perfectly.我只使用常规的 HTML select 测试了我的代码切换主题,它运行良好。 The input matched the selected option across the site.输入与整个站点中的选定选项匹配。

So, it must be materialize.js and whatever is going on in that file.因此,它必须是 materialize.js 以及该文件中发生的任何事情。 I tried looking at the code in that file to see if there was anything I could alter, but I'm really not at a level to discern what would need to be changed without breaking everything.我尝试查看该文件中的代码以查看是否有任何可以更改的内容,但我真的无法在不破坏所有内容的情况下辨别需要更改的内容。

For my project, this would constitute an annoying little bug.对于我的项目,这将构成一个烦人的小错误。

Here's my code:这是我的代码:

HTML HTML

<div class="input-field">
    <select name="theme" id="theme">
        <option value="auto">Auto</option>
        <option value="light">Light</option>
        <option value="dark">Dark</option>
    </select>
</div>

<script>
    const select = document.querySelector('select');
    M.FormSelect.init(select, {});
</script>

CSS CSS

:root {
    --dark-background-color: black;
    --dark-color: white;
}

body {
    /* Light theme */
    --background-color: white;
    --color: black;

    background-color: var(--background-color);
    color: var(--color);
}

body.theme-dark {
    --background-color: var(--dark-background-color);
    --color: var(--dark-color);   
}

@media (prefers-color-scheme: dark) {
    body.theme-auto {
        --background-color: var(--dark-background-color);
        --color: var(--dark-color);   
    }
}

.input-field input {
    color: var(--color);
}

JS JS

function applyTheme(theme) {
    document.body.classList.remove('theme-auto', 'theme-light', 'theme-dark');
    document.body.classList.add(`theme-${theme}`);
}

document.addEventListener('DOMContentLoaded', () => {
    const savedTheme = localStorage.getItem('theme') || 'auto';

    applyTheme(savedTheme);

    for (const optionElement of document.querySelectorAll('#theme option')) {
        optionElement.selected = savedTheme === optionElement.value;
    }

    document.querySelector('#theme').addEventListener('change', function() {
        localStorage.setItem('theme', this.value);
        applyTheme(this.value);
    });
});

Materialize JS for Select: EDIT: I think I've narrowed it down to the offending code.为 Select 实现 JS:编辑:我想我已经将其缩小到有问题的代码。

      /**
       * Handle Input Click
       */

    }, {
      key: "_handleInputClick",
      value: function _handleInputClick() {
        if (this.dropdown && this.dropdown.isOpen) {
          this._setValueToInput();
          this._setSelectedStates();
        }
      }

        // Add input dropdown
        this.input = document.createElement('input');
        $(this.input).addClass('select-dropdown dropdown-trigger');
        this.input.setAttribute('type', 'text');
        this.input.setAttribute('readonly', 'true');
        this.input.setAttribute('data-target', this.dropdownOptions.id);
        if (this.el.disabled) {
          $(this.input).prop('disabled', 'true');
        }

        this.$el.before(this.input);
        this._setValueToInput();

The solution may be not to use Materialize.解决方案可能是不使用 Materialise。 I guess I'm just wondering if anyone more familiar with the underlying mechanics of Materialize (its JS) could discern a fix here.我想我只是想知道是否有人更熟悉 Materialise 的底层机制(它的 JS)可以在这里发现一个修复。

Here's a GIF of the problem in action这是实际问题的 GIF

这是实际问题的 GIF

Added this to my JS.将此添加到我的 JS 中。

// added
const themeSelect = document.getElementById('theme');

const savedTheme = localStorage.getItem('theme');
if (savedTheme) themeSelect.value = savedTheme;

themeSelect.addEventListener('change', function () {
    localStorage.setItem('theme', this.value);
});

What is strange is that this solution seems redundant.奇怪的是,这个解决方案似乎是多余的。 The original JS from the question (not materialize.js) should be doing the same thing yet it doesn't but then add this and it finally takes control of the input away from the materialize.js file.问题中的原始JS(不是materialize.js)应该做同样的事情,但它没有做同样的事情,然后添加它,它最终控制了materialize.js文件中的输入。 Perhaps they can be merged without losing the fix.也许它们可以合并而不会丢失修复。 We may never know.我们可能永远不会知道。

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

相关问题 如何动态修改<select>在实现 css 框架中 - How to dynamically modify <select> in materialize css framework 如何基于选择的选择选项动态更改输入值属性? - How to change dynamically input value attribute based on select option selected? 使用物化 css 时如何动态创建 select 选项 - How to dynamically create select options when using materialize css 如何用文本输入动态替换 Materialize Select? - How to replace a Materialize Select with Text-Input dynamically? 如何在ejs中动态呈现输入的选定选项 - how to dynamically render the selected option of an input in ejs Materialise CSS - 多个 select 已禁用选定问题 - Materialize CSS - multiple select with disabled selected issue 如何动态创建带有选定选项的select - How to dynamically create select with selected option Reactive Forms - 设置选定的输入<option>的<select>FormControl 动态按条件 - Reactive Forms - Set the input of selected <option> of a <select> FormControl dynamically by condition 如何在多个 select 类型的用户选择的选项上动态添加输入字段 - How to dynamically add input fields on user selected option in multiple select type 我怎样才能把选择的选项 <select>从动态生成的表单到输入字段 - How can I put the selected option of a <select> from a dynamically generated form into an input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM