更新功能在包含所选选项数组的循环内不起作用。没有循环,它正在工作,我已经检查了数组包含正确的值。
HTML代码:
<div class="col-sm-4">
<select multiple class="form-control" id="studentcontainer">
</select>
</div>
JAVASCRIPT:
db.collection('Parent').get().then(snapshot => {
snapshot.forEach(doc => {
var data = '<option value="' + doc.data().uid + '">' + doc.data().student_name + " - " + doc.data().student_admno + '</option>';
document.getElementById('studentcontainer').innerHTML += data;
});
});
`
function assignStudent() {
var e = document.getElementById("driverscontainer");
var driverid = e.options[e.selectedIndex].value + "";
var selectedElement = document.getElementById('studentcontainer');
var selectedOptions = selectedElement.selectedOptions || [].filter.call(selectedElement.options, option => option.selected);
var selectedValues = [].map.call(selectedOptions, option => option.value);
for (index = 0; index < selectedValues.length; index++) {
console.log(selectedValues[index]);
var studentdriver = {
did : driverid,
sid : selectedValues[index]
}
db.collection("AssignedStudents").doc(driverid).set(studentdriver).then(function() {
alert("Student Assigned Successfully");
});
var docRef = firebase.firestore().collection("Parent").doc(selectedValues[index]).update({
isAssigned: true
});
}
alert("Students Assigned Successfully");
}