1
我想在firebase中存儲/推送輸入數據。使用Ajax保存輸入數據
表HTML
<form>
<input name="name" type="name" placeholder="Title" id="titleInput" />
<button type="submit" class="btn" onclick="submitPost()">Submit</button>
</form>
推輸入數據火力
var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
myDataRef.on('child_added', function(snapshot) {
var post = snapshot.val();
});
function submitPost(e) {
var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
var name = $('#titleInput').val();
myDataRef.push({name: name});
}
上面的代碼工作在這個jsfiddle。在我的情況下,我不能將https://cdn.firebase.com/v0/firebase.js放在網站標題中,所以我想通過Ajax嘗試。
HTML表格與jQuery的
$('header').prepend ('<form><input name="name" type="name" id="titleInput"/>
<button type="submit" onclick="submitPost()">Submit</button></form>');
我不是很熟悉,Ajax和下面的代碼轉化不工作。幫幫我?
$.ajax({
url: 'https://cdn.firebase.com/v0/firebase.js',
dataType: 'script',
cache: true,
success: function() {
var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
myDataRef.on('child_added', function(snapshot) {
var post = snapshot.val();
displayUserPost(post.name);
});
}
});
$.ajax({
url: 'https://cdn.firebase.com/v0/firebase.js',
dataType: 'script',
cache: true,
success: function submitPost(e) {
var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
var name = $('#titleInput').val();
myDataRef.push({name: name});
$('#postInput').val('');
e.preventDefault();
}
});