简体   繁体   English

如何在反应JS中单击按钮打开表单

[英]How to open form on button click in react JS

function openForm() {
    document.getElementById("myForm").style.display = "block";
}

function closeForm() {
    document.getElementById("myForm").style.display = "none";
}
    
<button class="openButton" onclick={openForm} style={openButton}>AI Chat</button>                
<button type="button" class="btn cancel" onclick={closeForm}>Close</button>

I am trying to open a form by onclick event in react, but it's not opening, I am new to react.我试图通过 onclick 事件在反应中打开一个表格,但它没有打开,我是新来的反应。 here attaching what the code..this is working fine with.这里附上什么代码..这很好用。 document.getElementById("myForm").style.display = "block"; document.getElementById("myForm").style.display = "block"; As by default chat window is none,we need to add STYLE display = block when using react,but am nit getting how to add.由于默认聊天 window 是 none,我们需要在使用 react 时添加 STYLE display = block,但不知道如何添加。

const [open, setIsOpen] = useState(false);
  const openForm = () => 
  {
    setIsOpen(true);
  };
<Button variant="primary" onClick={openForm} style={openButton} >IRB Chat</Button>  
      <form open={open}>
    <div class="chatPopup" id= "myForms" style={chatPopup}>
      <div class="formContainer" style={formContainer}>
        <span class="title1" style={title1}>IRB Chat</span>
        <label for="msg"><b>Message</b></label>                                         
       <iframe customFrameSection  style={customFrameSection} frameborder="1" id="AIChat" style={{border:'1px solid rgba(0,255,0,0.3)',width: "285px",height: "400px"}}></iframe>
       <button type="button" class="btn cancel" onclick={closeForm} style={cancelButton}>Close</button>
      </div>      
  </div>
      </form>

CSS:- const chatPopup = { display: "none", position: "fixed", bottom: "75px", right: "15px", border: '3px solid #F1F1F1', zIndex:9 }; CSS:- const chatPopup = { display: "none", position: "fixed", bottom: "75px", right: "15px", border: '3px solid #F1F1F1', zIndex:9 };

To do this with React, the best way is using State.要使用 React 做到这一点,最好的方法是使用 State。 If you do this with vanilla javascript, you don't need React.如果你用 vanilla javascript 做这个,你就不需要 React。

example with React and useState:使用 React 和 useState 的示例:

const [open, setIsOpen] = React.useState(false);

const openForm = () => setIsOpen(true);

<button onClick={openForm}> click me! </button>
<Form open={open}/>

hope helpfull =)希望有帮助 =)

const [open, setIsOpen] = useState(false);
  const openForm = () => setIsOpen(true);
    <button class="openButton"  onClick={() => {openForm}} style={openButton}>Chat</button>        
        <div class="chatPopup" id="myForm"  style={chatPopup}>
        <Form open={open}/>
          <div class="formContainer" style={formContainer}>
            <span class="title1" style={title1}>Chat</span>
            <label for="msg"><b>Message</b></label>                                         
           <iframe customFrameSection  style={customFrameSection} frameborder="1" id="AIChat" style={{border:'1px solid rgba(0,255,0,0.3)',width: "285px",height: "400px"}}></iframe>
           <button type="button" class="btn cancel" onclick={closeForm} style={cancelButton}>Close</button>
          </div>
          </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM