我有一个Dart脚本,该脚本从两个<input>
元素获取值并比较它们以检查它们是否相等。但是出于某种奇怪的原因,其中一个<input>
元素的值始终是""
(即使我在内部引入了文本)两个输入的代码都是相同的,并且对于其中一个输入,它的工作原理是完美的。
这些是输入:
Contraseña:<input maxlength="30" name="psswd" type="password"><br>
Confirmar contraseña:<input maxlength="30" name="psswd-confirm" type="password"><br>
这是Dart代码,用于检查它们的值是否相等(在用户单击按钮时调用)
InputElement passwordInput = querySelector('[name="psswd"]');
InputElement psswdConfirmInput = querySelector('[name="psswd-confirm"]');
if (passwordInput.value != psswdConfirmInput.value) {
showBlock(querySelector("#signup-psswd-error1"));
return;
}
该showBlock()
功能仅更改元素的样式即可display: block
,而我认为没有问题。
编辑整个表单的代码如下:
<form name="signup" id="form-signup" action="http://localhost:8080" method="post">
<span id="signup-email-error1">Esta dirección de correo electrónico ya ha sido registrada.</span>
<span id="signup-email-error2">Las dos direcciones de correo electrónico no coinciden.</span>
<span id="signup-psswd-error1">Las dos contraseñas no coinciden.</span>
<span id="signup-empty-error1">Debes completar todos los campos.</span>
Correo electrónico:<input maxlength="30" name="email" type="email" value=""><br>
Confirmar correo:<input maxlength="30" name="confirm-email" type="email" value=""><br>
Contraseña:<input maxlength="30" name="psswd" type="password"><br>
Confirmar contraseña:<input maxlength="30" name="psswd-confirm" type="password"><br>
Nombre y apellidos:<input maxlength="30" name="name" type="text" value=""><br>
<button id="signup-submit">Crear cuenta</button>
</form>
该<span>
元素是默认隐藏,只显示在错误的错误消息。
编辑HTML已经通过,没有任何错误,也没有警告w3c验证程序进行验证,因此可能不存在该错误。与错误相关的dart事件侦听器功能的代码如下:
signupSubmit.onClick.listen((e) {
e.preventDefault();
for (int i = 0; i < signupForm.children.length; i++) { //AUTOMATIC HIDING
if (signupForm.children[i] is SpanElement) {
hide(signupForm.children[i]);
}
}
for (int i = 0; i < signupForm.children.length; i++) { //CHECK FOR EMPTY FIELDS
Element element = signupForm.children[i];
if (element is InputElement) {
if (element.value == "") {
showBlock(querySelector("#signup-empty-error1"));
return;
}
}
}
InputElement emailInput = querySelector('[name="email"]');
InputElement emailConfirmInput = querySelector('[name="confirm-email"]');
if (emailInput.value != emailConfirmInput.value) {
showBlock(querySelector("#signup-email-error2"));
return;
}
InputElement passwordInput = querySelector('[name="psswd"]');
InputElement psswdConfirmInput = querySelector('[name="psswd-confirm"]');
hide(passwordInput);
hide(psswdConfirmInput);
print('passwordInput: ${passwordInput.value}');
print('confirm: ${psswdConfirmInput.value}');
if (passwordInput.value != psswdConfirmInput.value) {
showBlock(querySelector("#signup-psswd-error1"));
print('psswd different');
return;
}
var req = new HttpRequest();
req.onReadyStateChange.listen((e) {
if (req.readyState == HttpRequest.DONE) {
print(req.responseText);
if (req.responseText == "regComplete") {
hide(overlay);
Element newel1 = new HeadingElement.h1();
newel1.text = "¡Gracias por registrarte!";
Element newel2 = new HeadingElement.h2();
newel2.text = "Se ha enviado un correo electrónico a la dirección proporcionada.";
Element newel3 = new HeadingElement.h2();
newel3.text = "Haz click en el enlace recibido para verificar la dirección y confirmar el registro.";
Element newel4 = new HeadingElement.h4();
newel4.text = "Del equipo de CampL con ♡";
querySelector("#welcome")
..children.clear()
..children.add(newel1)
..children.add(newel2)
..children.add(newel3)
..children.add(newel4);
} else if (req.responseText == "mailTaken") {
showBlock(querySelector("#signup-email-error1"));
}
}
});
req.open('POST', signupForm.action);
req.send(JSON.encode(serializeForm(signupForm)));
print('Data submitted!');
});
在此之前的另一种形式中,还有另一个<input>
字段name
设置为“ psswd”,因此程序首先从该值中获取值<input>
。我已经更改了先前表单的名称(以及服务器端的代码),现在它可以正常工作了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句