【发布时间】:2021-08-29 06:14:30
【问题描述】:
我的 html :-
<form>
<div class="userPhoto"><img src="https://picsum.photos/300/700" alt=""></div>
<div class="name">
<div class="fN">
<p>First Name :</p>
<div class="ic"><input type="text" name="first_name">
<div class="icon">
<div class="error" style="display: none; visibility: hidden;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: block;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
<div class="fN">
<p>Last Name :</p>
<div class="ic"><input type="text" name="last_name">
<div class="icon">
<div class="error" style="display: none; visibility: hidden;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: block;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
</div>
<div class="email">
<div class="cont">
<p>Enter your email :</p>
<div class="ic"><input type="email" name="email">
<div class="icon">
<div class="error" style="display: block; visibility: visible;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: none;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
<div class="process">
<p class="pc">Checking Availability...</p>
<p class="ps">Email is available .</p>
<p class="pe">This Email is already in use .</p>
</div>
</div>
<div class="contact_no">
<div class="cont">
<p>Enter your contact number :</p>
<div class="ic"><input type="number" name="email">
<div class="icon">
<div class="error" style="display: none; visibility: hidden;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: block;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
<div class="process">
<p class="pc">Checking Availability...</p>
<p class="ps">contact no. is available .</p>
<p class="pe">This contact no. is already in use .</p>
</div>
</div>
<div class="userName">
<div class="cont">
<p>Choose an user name:</p>
<div class="ic"><input type="text" name="email">
<div class="icon">
<div class="error" style="display: block; visibility: visible;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: none;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
<div class="process">
<p class="pc">Checking Availability...</p>
<p class="ps">user name is available .</p>
<p class="pe">This user name is already in use .</p>
</div>
</div>
<div class="password">
<div class="cont">
<p>create an strong PASSWORD:</p>
<div class="ic"><input type="password" name="email">
<div class="icon">
<div class="error" style="display: none; visibility: hidden;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: block;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
</div>
<div class="conform_password">
<div class="cont">
<p>conform password:</p>
<div class="ic"><input type="password" name="email">
<div class="icon">
<div class="error" style="display: none; visibility: hidden;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg></div>
<div class="success" style="display: block;"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path>
</svg></div>
</div>
</div>
</div>
</div>
<div class="formSubmit">
<div class="button">
<p>Sign up</p>
</div>
</div>
</form>
我将 const form = useRef(null); 引用到 html form 元素。
但我需要 input 内的 .password class 值。
我现在在 React 中的表现如何
let passwordText = form.current.children[5].children[0].children[1].children[0].value;
但我认为这是不对的。因为它很长。而且我不愿意使用另一个变量作为密码参考。
有没有简单快捷的方法来实现这一点?
【问题讨论】:
-
在反应世界中,每个元素都可以有自己的
ref。为什么你坚持使用父 ref 来获取嵌套的子值,而不是在密码上使用新的 ref?也许最初的原因是错误的。
标签: javascript html reactjs react-hooks