简体   繁体   English

我正在尝试制作 javascript 表单项目。 与反应。 我想把 onChange function 放到输入里面。 但是怎么做?

[英]ı am trying to make javascript form project. with react. I want put onChange function to inside of input. But how?

I am trying to make form project with react but ı have a problem.我正在尝试使用 react 制作表单项目,但我遇到了问题。 when ı click to enter button, the button give true to console.当我单击进入按钮时,该按钮将 true 用于控制台。 even the form has not complete.连表格都没有完成。 So ı want ıf form ıs not complete or give error, the enter button give false to console.所以我想如果表单不完整或给出错误,输入按钮会给控制台错误。 else, enter button give true to console.否则,输入按钮将 true 赋予控制台。 But ı have to do with onChange.但我必须与 onChange 有关。 So how can ı do with onChange this?那么我怎么能用 onChange 这个呢?

    import React from "react";
import { useState } from "react";

const Contact = () => {
    const [button, setbutton] = useState(false);
    
    const buttonOnClick = () => {
        setbutton(true)
        console.log(`Form submitted, ${button}`);    
    }


    console.log(button);

    return(
<div className="main">
    <form >
    <div className="baslik">
     <div className="container center">
    <h1>Contact Form</h1>
    </div>
    </div>
        <div className="field" >
           <label className="text"> Name And Surname: </label>
           <input className="form" placeholder="Kerem Kurt" required />
        </div>
    

      <button type="submit" className="button" onClick ={() => buttonOnClick()}> Enter </button> 

React doesn't care if your form is complete or not. React 不在乎你的表单是否完整。 You clicked the button and it executed the onClick handler.您单击了该按钮,它执行了 onClick 处理程序。 If you want to print false.如果要打印假。 Please validate your form in onButtonClick() and then use setbutton appropriately.请在 onButtonClick() 中验证您的表单,然后适当地使用 setbutton。 Ie if you find that your form is invalid set it to false or else set it to true.即,如果您发现您的表单无效,请将其设置为 false 或将其设置为 true。

You have to manually do that.您必须手动执行此操作。 As you are not using any form validation library.因为您没有使用任何表单验证库。 Let me give you an example让我给你举个例子

 import React from "react"; import { useState } from "react"; const Contact = () => { const [button, setbutton] = useState(false); const [inputValue,setInputValue] = useState("") const buttonOnClick = () => { if(inputValue === ""){ setbutton(false) }else{ setbutton(true) } console.log(`Form submitted, ${button}`); } console.log(button); return( <div className="main"> <form> <div className="baslik"> <div className="container center"> <h1>Contact Form</h1> </div> </div> <div className="field" > <label className="text"> Name And Surname: </label> <input className="form" placeholder="Kerem Kurt" required value={inputValue} onChange={(e)=>setInputValue(e.target.value)/> </div> <button type="submit" className="button" onClick ={() => buttonOnClick()}> Enter </button> </form> </div>

暂无
暂无

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

相关问题 我正在尝试根据用户输入创建一个函数。 我在用我的代码做什么? - I am trying to create a function based on user input. What am I doing with my Code? 我正在尝试在 React 中制作折叠/手风琴。 但问题是,它不会触发我想要切换的特定内容 - I'm trying to make a collapse/accordion in React. But the problem is, it is not triggering the specific content that i want to toggle 将光标放在输入框内 onChange 在 React 中 - Put cursor inside input box onChange in React 尝试测试反应组件形式的onChange函数 - Trying to test onChange function of react component form 带有 React 的会话表单。 如何进行多项选择? - Conversational Form with React. How to make multiple choice? 如何让按钮在 React 中触发文件输入 onChange? - How do I make a button trigger a file input onChange in React? 我想在 React js 中的单选按钮切换时显示和隐藏表单。 我正在尝试如何使用反应钩子来隐藏或显示 onChange 上的组件甚至 - I want to show and hide a form on toggle of a radio button in React js . Im trying how to use react hooks to hide or show a component on onChange even input onChange 正在清除 React 中 DOM 中的所有元素。 输入表单与按钮配合使用效果很好,但似乎无法在第一次加载时使用 onChange - input onChange is wiping all elements from DOM in React. The input form works well with a button, but can't seem to work onChange on the first load 如何禁用带有 javascript onchange 功能的表单? - How can I disable a form with javascript onchange function? 我正在尝试使用 javascript 进行表单验证,但 email 验证不起作用 - I am trying to make form validation with javascript but email validation not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM