如何在Framework7中获取输入数据?

时间:2016-12-20 09:19:33

标签: javascript cordova html-framework-7

我在Framework7中使用cordova,我的要求如下:

  

enter image description here

如你所见,我想存储我的inputs'形式的价值:

在我的my-app.js

// Initialize your app
var myApp = new Framework7();

// Export selectors engine
var $$ = Dom7;

// Add views
var view1 = myApp.addView('#view-1');
var view2 = myApp.addView('#view-2', {
    // Because we use fixed-through navbar we can enable dynamic navbar
    dynamicNavbar: true
});
var view3 = myApp.addView('#view-3');
var view4 = myApp.addView('#view-4');


// 这个是下来刷新
var ptrContent = $$('.pull-to-refresh-content');



/*设置界面*/

$$('.save-storage-data').on('click', function() {
    var storedData = myApp.formStoreData('my-info-form', {
                                     'name': 'John', // this value should be the input's value, I just write for test here
                                     'address':'地址',  // this value should be the input's value, I just write for test here
                                     'page':'page',  // this value should be the input's value, I just write for test here
                                     'tel':'139',  // this value should be the input's value, I just write for test here
                                     'email': 'john@doe.com',  // this value should be the input's value, I just write for test here
                                     'gender': 'female',  // this value should be the input's value, I just write for test here
                                     'isAcceptPushNotification': ['yes'],  // this value should be the input's value, I just write for test here
                                     'birthday': ''  // this value should be the input's value, I just write for test here
                                     });
    alert('保存成功')
});

如您所见,$$('.save-storage-data').on('click', function()功能正是我想要的,我想将form信息保存到我的应用中。

此外,我应该判断所有输入是否都填充了值,然后保存数据。

1 个答案:

答案 0 :(得分:0)

让我们遍历所有表单输入字段并在保存之前检查是否所有表单都已填充:

$$('.save-storage-data').on('click', function() {

    $$('.save-storage-data input').each(function(i, obj) {
        if (!$$(this).val()) {
           myApp.alert("Please fill all the required fields.");
           return false;
        }
    });

    var storedData = myApp.formStoreData('my-info-form', {
                                     'name': 'John', // this value should be the input's value, I just write for test here
                                     'address':'地址',  // this value should be the input's value, I just write for test here
                                     'page':'page',  // this value should be the input's value, I just write for test here
                                     'tel':'139',  // this value should be the input's value, I just write for test here
                                     'email': 'john@doe.com',  // this value should be the input's value, I just write for test here
                                     'gender': 'female',  // this value should be the input's value, I just write for test here
                                     'isAcceptPushNotification': ['yes'],  // this value should be the input's value, I just write for test here
                                     'birthday': ''  // this value should be the input's value, I just write for test here
                                     });
    alert('保存成功')
});