iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
自我挑戰組

從零開始學習React 系列 第 14

Day14 React- Forms(2)

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20210929/20139800uj15HiDvdE.png

import React , {useState} from 'react';

const Forms = () => {
    
    const [fullName, setFullName] = useState({
        name:"",
        phone:"",
        email:"",
    });

    const inputEvent = (event)=>{       

       const {value,name} = event.target;

       setFullName((preValue)=>{
           
        if (name ==="name" ){
           return{
               name:value,
               phone:preValue.phone,
               email:preValue.email,
           };
        } else if (name==="phone"){
            return{
                name:preValue.name,
                phone:value,
                email:preValue.email,            
           };
        } else if (name==="email"){
            return{
                name:preValue.name,
                phone:preValue.phone,
                email:value,            
           };
        }
       });       
    };

    const onSubmits = (event) => {
        event.preventDefault();
    };

    const goBack = ()=>{
        setFullName("");
    }

       
    return (
        <div>
           <div className="container">
      <form onSubmit = {onSubmits} >
      <h2>Login Form</h2>
      <input 
               type="text" 
               placeholder="Enter You Name" 
               name="name"
               onChange={inputEvent} 
               value={fullName.name} />
       
      <input 
               type="text" 
               placeholder="Enter You Phone" 
               name="phone"
               onChange={inputEvent} 
               value={fullName.phone} />

      <input 
               type="email" 
               placeholder="Enter You E-mail" 
               name="email"
               onChange={inputEvent} 
               value={fullName.email}
               autoComlete="off" />


      <button type="submit" onDoubleClick={goBack}>Login</button>  
  </form>
</div>

<div className="demo">
   <div className="wrapper">
   
       <h2>Personal Information</h2>
       <h3 > Name:{fullName.name}</h3>
       <h3 > Phone:{fullName.phone}</h3>
       <h3 > E-mail:{fullName.email}</h3>
   </div> 
</div>
        </div>
    );
};

export default Forms

上一篇
Day13 React- Forms(1)
下一篇
Day15 Todo List
系列文
從零開始學習React 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言