Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM Input Email value

คุณสมบัติ value ของ HTML DOM Input Email ใช้สำหรับดึงหรือกำหนดค่าของช่องป้อนอีเมล (<input type="email">)

ไวยากรณ์ (Syntax)

การดึงค่า value

inputEmailObject.value

การกำหนดค่า value

inputEmailObject.value = 'String'

ตัวอย่างการใช้งาน

ตัวอย่างฟอร์มล็อกอินด้วยอีเมล - เลือกผู้ใช้จากปุ่ม แล้วตรวจสอบก่อนเข้าสู่ระบบ

<!DOCTYPE html>
<html>
<head>
<title>Input Email value</title>
<style>
  form {
    width: 70%;
    margin: 0 auto;
    text-align: center;
  }
  * {
    padding: 2px;
    margin: 5px;
  }
  input[type="button"] {
    border-radius: 10px;
  }
</style>
</head>
<body>
<form>
  <fieldset>
    <legend>Email-value</legend>
    <label for="EmailSelect">อีเมล: <input type="email" id="EmailSelect"> 
    <input type="button" onclick="getUserEmail('david')" value="David"> 
    <input type="button" onclick="getUserEmail('shasha')" value="Shasha"><br> 
    <input type="button" onclick="login()" value="Login"> 
    </label> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputEmail = document.getElementById("EmailSelect"); 
  function getUserEmail(userName) { 
    if(userName === 'david') 
      inputEmail.value = 'davidMiller@MNC.com'; 
    else 
      inputEmail.value = 'shashaGreen@MNC.com'; 
  } 
  function login() { 
    if(inputEmail.value !== '') 
      divDisplay.textContent = 'Successful Login. Hello ' + inputEmail.value.split("@")[0]; 
    else 
      divDisplay.textContent = 'Enter Email Id'; 
  } 
</script> 
</body> 
</html>

ผลลัพธ์

ก่อนคลิกปุ่ม Login (ช่องว่าง) - แสดง "Enter Email Id"

หลังคลิกปุ่ม "David" - ช่องกรอกแสดง "davidMiller@MNC.com"

หลังคลิกปุ่ม Login พร้อมอีเมล - แสดง "Successful Login. Hello davidMiller"

ข้อดีของ type="email": เบราว์เซอร์จะตรวจสอบรูปแบบอีเมลอัตโนมัติ (validation) และแสดงคีย์บอร์ดที่เหมาะสมบนอุปกรณ์เคลื่อนที่