2014-02-21 52 views
1

我有一個飛鏢腳本從兩個<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,我不認爲有問題。

EDIT 整個表格中的代碼如下:

<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驗證程序的驗證,所以可能錯誤不存在。飛鏢事件監聽器功能相關的錯誤代碼如下:

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!'); 
}); 

回答

1

在另一形式中,這是這一個有另一個<input>場與name設置爲 「psswd」,所以前該計劃從第一個<input>獲得價值。 我已經改變了以前的表單(和服務器端的代碼)的名稱,現在它工作沒有問題。

0

我試過你的代碼,它的工作原理。

我在querySelectorif之間添加了以下代碼,它會打印我輸入的值。

print('passwordInput: ${passwordInput.value}'); 
print('confirm: ${psswdConfirmInput.value}'); 

輸出:

passwordInput:RR
確認:RR

+0

這就是當我看到即使兩個密碼相同時錯誤消息出現,但passwordInput的值始終爲''「'(因此在輸入」rr「到輸入時的代碼輸出是''passwordInput:「」confirm:rr「') – dieortin

+0

我懷疑在輸入密碼之前你的標記中有一些錯誤,這會使字段變得很奇怪。 –

+0

我用整個表單的代碼更新了我的問題。 – dieortin