MDL jquery: label overlaps the content into a input field

ぐ巨炮叔叔 提交于 2019-12-07 20:33:21

问题


I use Firefox Version 40.0.3/Chrome 45.0.2454.85 and i have the following problem:
The placeholder/label overlaps the content, if i use jQuery.

Javascript:

$("#eventAddKurzLB").val("test");

HTML:

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text" id="eventAddKurzLB" />
    <label class="mdl-textfield__label" for="eventAddKurzLB">Kurzbezeichnung:</label>
    <span class="mdl-textfield__error">Es sind nur Zahlen, Buchstaben und Bindestriche erlaubt!</span>
</div>

Example:

Live-Example:
https://jsfiddle.net/vutLb9ut/2/


回答1:


The problem isn't jQuery, but rather the fact that the component is unaware of the changes you're making (programmatic changes to an HTML element don't trigger events), so it doesn't know it should change its looks. You can test this by manually typing into the box; that should work correctly, even if jQuery is loaded.

You can use the element's API for changing the text instead, by doing:

$("#my-textfield").get(0).MaterialTextfield.change('test');

(where my-textfield is the outer label element), provided the element is done upgrading. The other option is to perform the change directly, as you are doing, but triggering a "dirtiness" check on the element:

$("#my-textfield").get(0).MaterialTextfield.checkDirty();

Hope this helps!




回答2:


If you want to use both then , you have to use <br/>. Something like this..

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">

<label class="mdl-textfield__label" for="eventAddKurzLB"> Kurzbezeichnung:</label> 
<span class="mdl-textfield__error">Es sind nur Zahlen, Buchstaben und Bindestriche erlaubt!</span>
<br/>

<input class="mdl-textfield__input" type="text" id="eventAddKurzLB" /></div>

check fiddle -> https://jsfiddle.net/vutLb9ut/3/



来源:https://stackoverflow.com/questions/32579980/mdl-jquery-label-overlaps-the-content-into-a-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!