0

我正在预填充一些文本字段并且边框不可见。 在此处输入图像描述

例如,我正在使用以下 JavaScript 来预填充字段,但在单击/取消单击该字段之前不会呈现大纲。我试过了

var doc = document;
doc.getElementById("other-number-tf-ex-input").parentElement.classList.add("mdc-text-field--upgraded");
doc.getElementById("other-number-tf-ex-input").nextElementSibling.classList.add("mdc-text-field__label--float-above");
doc.getElementById("other-number-tf-ex-input").value = values[1].otherPhoneExtensionCode;
window.mdc.autoInit();

HTML是:

<div class="account-telephone-outlined-wrapper">
    <div class="mdc-text-field mdc-text-field--outlined mdc-text-field--dense tel-medium" data-mdc-auto-init="MDCTextField">
        <input required pattern="^[0-9]*$" type="text" id="cellular-number-tf-cc-input" class="mdc-text-field__input" aria-controls="cellular-number-tf-cc-message">
        <label for="cellular-number-tf-cc-input" class="mdc-text-field__label">1</label>
        <div class="mdc-text-field__outline">
            <svg>
                <path class="mdc-text-field__outline-path" d=""/>
            </svg>
        </div>
        <div class="mdc-text-field__idle-outline"></div>
    </div>
    <p class="mdc-text-field-helper-text mdc-text-field-helper-text--validation-msg" id="cellular-number-tf-cc-message">
        00000
    </p>
</div>
4

2 回答 2

0

您应该显式实例化一个 MDCTextField 对象,然后设置值:

var doc = document;

var field = new mdc.textField.MDCTextField(doc.getElementById("other-number-tf-ex-input-container"));
field.value = values[1].otherPhoneExtensionCode;

这对我有用并触发浮动标签。

注意other-number-tf-ex-input-container id:它必须指向容器 mdc-text-field div。

于 2018-03-29T15:14:52.557 回答
0

我只设置了正常的禁用文本字段配置(禁用类和输入的禁用属性),但以编程方式设置值,例如:

myMDCtextfield.value = "myvalue";

这会触发浮动标签:)

于 2019-08-12T16:03:55.070 回答