我怎样才能将数据添加到firebase数据库中

时间:2018-03-12 11:10:43

标签: javascript html firebase firebase-realtime-database

我想将已选中的复选框值添加到数据库中的timesession字段中,但我不知道如何对其进行编码。 一个字段可以存储多个数据吗? 如果可能存入该字段,那么我该如何将其检索出来?

var firebaseRef = firebase.database().ref('facility');

function submit(){
//how do i call the checkboxes value in the ul
firebaseRef.push().set();

}
<label style="color: #f2f2f2">Time Session</label> <br/>
    	
	<ul>
   <li><label for="chk1"><input type="checkbox" name="chk1" id="chk1" >09:00-11:00</label></li>
   <li><label for="chk2"><input type="checkbox" name="chk2" id="chk2" >12:00-14:00</label></li>
   <li><label for="chk3" ><input type="checkbox" name="chk3" id="chk3" >15:00-17:00</label></li>
   <li><label for="chk4"><input type="checkbox" name="chk4" id="chk4">18:00-20:00</label></li>
   
  </ul>
 <br/>
 <button onclick="submit()">Submit</button>
 
 
  
  <script src="https://www.gstatic.com/firebasejs/4.10.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyCtZEZ3LR-cOsEJXxL5JxCxOLlGZAgqgzw",
    authDomain: "communitysys-83bda.firebaseapp.com",
    databaseURL: "https://communitysys-83bda.firebaseio.com",
    projectId: "communitysys-83bda",
    storageBucket: "communitysys-83bda.appspot.com",
    messagingSenderId: "283084495842"
  };
  firebase.initializeApp(config);
  console.log(firebase);
  
  
  
</script>

1 个答案:

答案 0 :(得分:1)

<\ n>在复选框内,您需要添加代表复选框价值的value,如下所示:

<input type="checkbox" class="checkbx" name="chk1" id="chk1" value="09:00-11:00">

更多信息:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

然后,您需要检索它:

var checkedValue = document.querySelector('.checkbx:checked').value;
var firebaseRef = firebase.database().ref('facility');
firebaseRef.push().set({
 timesession: checkedValue 
  });

更多信息:

https://firebase.google.com/docs/database/web/read-and-write