我有一个JavaScript函数,当点击一个HTML按钮时,它应该被执行。表单的HTML是
<form id="inputData">
Email:<br>
<input id="email" type="email" name="email"><br>
Username:<br>
<input id="username" type="text" name="username"><br>
Password:<br>
<input id="password" type="password" name="password"><br>
<input id="submit" type="submit" value="submit" onClick="getData()">
</form>
相关的js代码是
<script type="text/javascript">
function getData() {
var input = document.getElementById("inputData");
var email = input[0].value;
var user = input[1].value;
var pass = input[2].value;
sendData(user,email,pass)
}
function sendData(user,email,pass) {
var fireRef = firebase.database().ref("users/" + user);
fireRef.set({
emailAddress: email,
password: pass,
});
}
</script>
在调试器模式下,单击submit
按钮时执行该功能,并且没有错误,所有表单数据都已正确收集,但数据永远不会发送到Firebase数据库。但是,如果我通过控制台调用sendData
函数并在参数中手动提供数据,则它可以完美地运行。谁能想到为什么会发生这种情况?
答案 0 :(得分:2)
onClick
事件未触发
试
<form id="inputData" onsubmit="getData()">
Email:<br>
<input id="email" type="email" name="email"><br>
Username:<br>
<input id="username" type="text" name="username"><br>
Password:<br>
<input id="password" type="password" name="password"><br>
<input id="submit" type="submit" value="submit">
</form>