我的单选按钮值出现问题,但没有插入数据库。我使用的是java-script和firebase实时数据库。谢谢你的回答。
function saveData() {
var fname = document.getElementById("fname");
var lname = document.getElementById("lname");
var selectedGender;
document.getElementsByName("gender").forEach(function(elm) {
if (elm.checked) {
selectedGender = elm.value;
}
})
var bDay = document.getElementById("bDay");
var moNum = document.getElementById("moNum");
var emailAdd = document.getElementById("emailAdd");
var username = document.getElementById("username");
var password = document.getElementById("password");
insertData(fname.value, lname.value, selectedGender.value, bDay.value,
moNum.value, emailAdd.value, username.value, password.value)
}
function insertData(fname,lname,selectedGender,bDay,moNum,emailAdd,username,password){
var firebaseRef = firebase.database().ref("users");
firebaseRef.push({
Firstname: fname,
Lastname: lname,
Gender: selectedGender,
Birthday: bDay,
Mobile: moNum,
Email: emailAdd,
Username: username,
Password: password
});
console.log("Insert Success");
signUp();
}

Gender : </td><td>
<input type="radio" name="gender" id="gender" value="male" />Male
<input type="radio" name="gender" id="gender" value="female" />Female </td></tr> <br/>
&#13;
答案 0 :(得分:1)
要从单选按钮获取值,您需要循环选择单选按钮并确定选中哪一个,如下所示: Get Radio Button Value with Javascript
或者你的情况:
var selectedGender;
document.getElementsByName("gender").forEach(function(elm) {
if (elm.checked) {
selectedGender = elm.value;
}
})