我正在使用以下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>
答案 0 :(得分: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。
答案 1 :(得分:0)
我仅设置常规的禁用文本字段配置(禁用的类和输入的disabled属性),但以编程方式设置该值,例如:
myMDCtextfield.value = "myvalue";
然后触发浮动标签:)