zoukankan      html  css  js  c++  java
  • [HTML 5] Access form and elements

    import './assets/css/style.css';
    
    const app = document.getElementById('app');
    app.innerHTML = `
      <h1>JavaScript DOM</h1>
      <form name="order">
        <label>
          Your name
          <input type="text" name="fullname">
        </label>
        <label>
          Your email
          <input type="text" name="email">
        </label>
      </form>
    `;
    
    const form = document.forms.order;
    
    const {fullname, email} = form.elements;
    
    function handleInput(event) {
      // access the value
      console.log(event.target.value);
    
      // access the form
      console.log(event.target.form);
    }
    
    fullname.addEventListener('input', handleInput);

    1. You can use document.forms to access form, by given form a name, you can access that form by name

    const form = document.forms.order;

    2. Inside event handler, you can also get form reference by:

      // access the form
      console.log(event.target.form);

    3. Access the elements of the form:

    const {fullname, email} = form.elements;
  • 相关阅读:
    spring cloud项目搭建
    获取iframe的window对象
    数学杂谈 #7
    [AGC023D] Go Home
    JOISC 2021 部分题解
    [NOI2017]泳池
    [NOI2016] 循环之美
    [NOI2016] 优秀的拆分
    [LG P3676]小清新数据结构题
    [ARC113F]Social Distance
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13154571.html
Copyright © 2011-2022 走看看