2016-11-09 48 views
2

我正在尝试使用JQuery更改MDL文本字段文本。但是,出现了一些问题,首先标签不是动画UP,对于基本文本字段,占位符文本不会消失。其次,如果一个文本字段被标记为required,通过JQuery设置它的值后,该字段保持为红色,就像没有放置在那里一样。 JSFiddle用JQuery动态更改MDL文本字段文本

HTML:

<dialog id="adicionar-editar-campista" class="mdl-dialog" style="text-align: center"> 
    <div class="mdl-dialog__content"> 
     <h4>Ficha de Cadastro</h4> 
     <p style="color: red">Campos em vermelho são obrigatórios.</p> 

     <form action="#"> 
      <div id="cadastro-numero-ficha" class="mdl-textfield mdl-js-textfield"> 
       <input id="numero-ficha" class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?"> 
       <label class="mdl-textfield__label" for="numero-ficha">N&#186; Ficha</label> 
       <span class="mdl-textfield__error">Apenas numeros!</span> 
      </div> 

      <div id="cadastro-nome-ficha" class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> 
       <input id="nome-ficha" class="mdl-textfield__input" type="text" required> 
       <label class="mdl-textfield__label" for="nome-ficha">Nome Completo</label> 
      </div> 
     </form> 
    </div> 
</dialog> 

的Javascript:

//Do something 
$("#numero-ficha").val(value.number); 
$("#nome-ficha").val(value.username); 
//Do something 

回答

2

我认为这是你在找什么

$('#numero-ficha')[0].parentElement.MaterialTextfield.change("15"); 
$('#nome-ficha')[0].parentElement.MaterialTextfield.change("some text"); 

Fiddle here

+0

没错!非常感谢你。 –