Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

javascript - 在React.js中使用preventDefault(Using preventDefault in React.js)

The validation is working correctly, however when the user fails the validation the default action is still carried out.(验证工作正常,但是当用户验证失败时,仍将执行默认操作。)

So validation fails and the joke is still returned.(因此验证失败,并且该笑话仍然返回。) If validation fails, the Joke should not be returned.(如果验证失败,则不应返回该笑话。) I have tried to use preventDefault but no luck.(我试图使用preventDefault但没有运气。) import React, { useState } from "react"; function Search() { const [joke, setJoke] = useState() const [firstname, setFN] = useState("sharoze") const [lastname, setLN] = useState("khan") const newJoke = (first, last) => { fetch(`http://api.icndb.com/jokes/random?firstName=${first}&lastName=${last}`) .then(result => result.json()) .then(result2 => { console.log(result2) setJoke(result2.value.joke) }) } function validateForm() { var firstname = document.getElementsByName("firstname")[0].value; var lastname = document.getElementsByName("lastname")[0].value; if (firstname === "" && lastname === "") { alert("Please enter atleast one name"); } else if (!(/^[a-zA-Z]+$/.test(firstname + lastname))) { alert("'Only alphabets allowed'"); } } return ( <div className="jokeForm" > <form name="searchForm" > <input type="text" name="firstname" placeholder="First name" value={firstname} onChange={(e) => setFN(e.target.value)} /> <input type="text" name="lastname" placeholder="Last name" value={lastname} onChange={(e) => setLN(e.target.value)} /> </form> <button id="button" onClick={(e) => { validateForm(newJoke(firstname, lastname)); return false; }}>click here for a personalised chuckle</button> <h3>{joke}</h3> </div > ) } export default Search; sorry the code has been a little butchered!(抱歉,该代码已被取消了!)   ask by Sharoze Khan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The main problem I can see is that you don't pass event argument from onClick to your validation function, hence preventDefault() function is not working.(我可以看到的主要问题是您没有将event参数从onClick传递到验证函数,因此preventDefault()函数无法正常工作。)

Try to pass event argument to validation function and then use this argument in function to apply preventDefault() .(尝试将event参数传递给验证函数,然后在函数中使用此参数以应用preventDefault() 。) --Update--(-更新-) You don't need to use preventDefault() at all.(您根本不需要使用preventDefault() 。) Just modify your validation function to call new joke only if validation is passed.(只需将验证功能修改为仅在通过验证后才发出新笑话。) import React, { useState } from "react"; function Search() { const [joke, setJoke] = useState() const [firstname, setFN] = useState("sharoze") const [lastname, setLN] = useState("khan") const newJoke = (first, last) => { fetch(`http://api.icndb.com/jokes/random?firstName=${first}&lastName=${last}`) .then(result => result.json()) .then(result2 => { console.log(result2) setJoke(result2.value.joke) }) } function validateForm() { var firstname = document.getElementsByName("firstname")[0].value; var lastname = document.getElementsByName("lastname")[0].value; if (firstname === "" && lastname === "") { alert("Please enter atleast one name"); return false; } else if (!(/^[a-zA-Z]+$/.test(firstname + lastname))) { alert("'Only alphabets allowed'"); return false; } newJoke(firstname, lastname); } return ( <div className="jokeForm" > <form name="searchForm" > <input type="text" name="firstname" placeholder="First name" value={firstname} onChange={(e) => setFN(e.target.value)} /> <input type="text" name="lastname" placeholder="Last name" value={lastname} onChange={(e) => setLN(e.target.value)} /> </form> <button id="button" onClick={() => validateForm()}>click here for a personalised chuckle</button> <h3>{joke}</h3> </div > ) } export default Search;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...