Typing in TextField doesn't make label float

徘徊边缘 提交于 2020-01-25 06:08:44

问题


I have made a form with Material Design components.

My TextField floating labels float when AutoFill fills the inputs, but not when a user types in them.

function fillFields() {
  $("#passwordTextField").val("test");
  $("#usernameTextField").val("test");
}
p.highlight-red-on-error {
  transition: color 180ms cubic-bezier(0.4, 0, 0.2, 1);
}

div.mdc-text-field--invalid~p.highlight-red-on-error {
  color: #b00020;
}
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <link rel="stylesheet" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">
  <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>

<body>

  <div>
    <div class="mdc-text-field">
      <input type="text" id="usernameTextField" name="username" class="mdc-text-field__input" pattern="[A-Za-z]*" required minlength=3 maxlength=32 data-mdc-auto-init="MDCTextField">
      <label for="usernameTextField" class="mdc-floating-label">Username</label>
      <div class="mdc-line-ripple"></div>
    </div>
    <p class="highlight-red-on-error">Username must be between 3 and 32 characters and contain only alphanumeric characters.</p>
  </div>


  <br>

  <!-- password field -->
  <div>
    <div class="mdc-text-field" data-mdc-auto-init="MDCTextField">
      <input type="password" id="passwordTextField" name="password" class="mdc-text-field__input" pattern="[A-Za-z!%^&*$?~`=+-_()]*" required minlength=8>
      <label for="passwordTextField" class="mdc-floating-label">Password</label>
      <div class="mdc-line-ripple"></div>
    </div>
    <p class="highlight-red-on-error">Password must be at least 6 characters. Some special charcaters are not allowed</p>
  </div>

  <p>
    Typing in the fields doesn't float the labels, but using autofill does.
  </p>
  <button onClick="fillFields()">Fill fields (doesn't float labels)</button>

  <script>
    window.mdc.autoInit();
    // from https://github.com/material-components/material-components-web/tree/master/packages/mdc-auto-init#using-as-part-of-material-components-web
  </script>
</body>

As you can see, neither editing the fields through JavaScript, nor typing in them floats the label. Only using auto-fill does. In addition, the autoInit(); doesn't seem to work.


回答1:


I found that using autoInit(); didn't work for some reason, so I had to manually initialise the components.

An example (using jQuery) for TextFields is:

$('.mdc-text-field').each(function (i, obj) {
    mdc.textField.MDCTextField.attachTo(obj);
});

The above code applies the correct properties and methods, etc. for all items with the class mdc-text-field



来源:https://stackoverflow.com/questions/52020280/typing-in-textfield-doesnt-make-label-float

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