javascript - 从 OnClick 按钮显示表单
问题描述
当我单击按钮时,我试图将表单显示为弹出窗口。我是 React 的新手,我真的不知道从哪里开始。
这是我要显示的 React 组件:
import React, {useState} from 'react';
const initialFormValues = {
firstName: '',
lastName: '',
email: '',
phone: '',
message: ''
}
export default function Contact(){
//set up state
const [contacts, setContacts] = useState([]);
const [formValues, setFormValues] = useState(initialFormValues);
//helper function to trach changes in the input
const inputChange= (name, value) => {
setFormValues({...formValues, [name]: value})
}
const onChange = e => {
const {name, value} = e.target;
inputChange(name, value);
}
//post a new contact to the backend eventually, right now just display the contact to the DOM
const postNewContact = newContact => {
setContacts([newContact, ...contacts])
setFormValues(initialFormValues);
}
const formSubmit = () => {
const newContact = {
firstName: formValues.firstName.trim(),
lastName: formValues.lastName.trim(),
email: formValues.email.trim(),
phone: formValues.phone.trim(),
message: formValues.message.trim()
}
postNewContact(newContact)
}
const onSubmit = e => {
e.preventDefault();
formSubmit();
}
return (
<div className='contact container'>
<h1>This is the contact component</h1>
<form id='contact-form' onSubmit={onSubmit}>
<div className='form-group submit'>
<button id='contact-btn'>SUBMIT CONTACT INFO</button>
</div>
<div className='form-group inputs'>
<label>
First Name:
<input
type='text'
value={formValues.firstName}
onChange={onChange}
name="firstName"
/>
</label>
<label>
Last Name:
<input
type='text'
value={formValues.lastName}
onChange={onChange}
name="lastName"
/>
</label>
<label>
Email:
<input
type='email'
value={formValues.email}
onChange={onChange}
name="email"
/>
</label>
<label>
Phone Number:
<input
type='text'
value={formValues.phone}
onChange={onChange}
name="phone"
/>
</label>
<label>
Message:
<input
type='text'
value={formValues.message}
onChange={onChange}
name="message"
/>
</label>
</div>
</form>
</div>
)
}
当我单击“联系我们”链接时,我想将此组件显示为弹出窗口:
import React from 'react';
import './footer.css'
function Footer()
{
return (
<div className="Footer">
<div className="container">
<div className="row">
<div className="col">
<h4 className="block ">About Us</h4>
</div>
<div className="col">
<h4 className="block" >Contact</h4>
</div>
<div className="col">
<h4><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/faq.html"}>COVID-19 FAQ</a></h4>
</div>
<div className="col">
<h4 className="block"><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/if-you-are-sick/quarantine.html"}>CDC Guidelines</a></h4>
</div>
</div>
</div>
</div>
);
}
export default Footer;
我将如何做到这一点?我希望使用以下表单在单击时显示弹出窗口,并且我希望在用户单击提交按钮时弹出窗口消失。
解决方案
您可以使用 HTML + CSS + Javascript 创建弹出窗口,如链接 Custom Popup中所示,或使用任何 UI 框架(如Ant Design)并使用 Modal 组件。
btn.onclick = function() {
modal.style.display = "block";
}
推荐阅读
- python - 如何将长/纬度整数从谷歌历史 JSON 转换为谷歌地图格式?
- mule - Postman 不考虑以 # 开头的查询参数值
- three.js - ThreeJS - 如何在“mouseout”上缩小相交对象
- python - 在tkinter中单击按钮上的两个脚本之间的链接?
- laravel - Laravel - 如何从与数据透视表相关的表中获取数据
- c++ - 将用户输入的术语与 txt 文件中的列表进行比较
- google-chrome-extension - 如何重新加载后台脚本?
- c++ - 检查二维向量中是否存在元素
- c# - 如何确保使用泛型在工厂中实例化对象的设计时检查?
- php - xmapp 仅显示后端代码