【问题标题】:How can I be able to add data into firebase database如何将数据添加到 firebase 数据库
【发布时间】:2018-03-13 21:22:55
【问题描述】:

我想将选中的复选框值添加到数据库中的 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>

【问题讨论】:

  • 这个答案对你有帮助吗?
  • 我正在尝试代码
  • 好的,随时通知我!
  • 这是什么错误 Uncaught TypeError: Cannot read property 'value' of null

标签: javascript html firebase firebase-realtime-database


【解决方案1】:

在复选框内,您需要添加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

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2017-12-28
    相关资源
    最近更新 更多