phonegap存储单选按钮值为javascript中的sqlite

时间:2016-01-12 11:43:58

标签: javascript html sqlite cordova

我正在尝试开发一个带有多项选择问题的phonegap应用程序,可以选择单选按钮。 HTML代码是:

        <p>First question</p>
        <form action="question.js" method="post">
            <label><input type="radio" id="mans1a" name="Mans1" value="excellent">option a</label>     
            <label><input type="radio" id="mans1b" name="Mans1" value="good">option b</label>        
            <label><input type="radio" id="mans1c" name="Mans1" value="average">option c</label>
            <label><input type="radio" id="mans1d" name="Mans1" value="poor">option d</label>    
        </form>
        <p>second question?</p>
        <form action="question.js" method="post">
            <label><input type="radio" id="mans2a" name="Mans2" value="yes">yes</label>
            <label><input type="radio" id="mans2b" name="Mans2" value="no">no</label>
        </form>

这是javascript:

var db = window.openDatabase("test","1.0","Test DB",2000000);
 function MediAns() 
 {
    db.transaction(PopulateDB, errorCB, successCB);
 }
function PopulateDB(tx)
{
    tx.executeSql('CREATE TABLE IF NOT EXISTS medicalshop (medicalshop_rating TEXT)'); 
    tx.executeSql('INSERT INTO medicalshop VALUES ("-MEDICAL SHOP-")');
    tx.executeSql('INSERT INTO medicalshop VALUES ("Excellent")');
    tx.executeSql('INSERT INTO medicalshop VALUES ("Good")');
    tx.executeSql('INSERT INTO medicalshop VALUES ("Average")');
    tx.executeSql('INSERT INTO medicalshop VALUES ("Poor")');
}
function queryDB(tx)
{
    tx.executeSql('SELECT * FROM medicalshop', querySuccess, errorCB);
}
function errorCB(tx, err) 
{
    alert("Error Processing SQL: "+err);
}

function successCB()
{
        var db = window.openDatabase("test", "1.0", "Test DB", 200000);
        db.transaction(queryDB, errorCB);
}

function querySuccess(tx, results) 
{
     var len = results.rows.length;
     alert(len);
     for (var i=0; i<len; i++)
     {
            var a = results.rows.item(i)['medicalshop_rating'];
            alert(a);
     }
}

我想知道如何将单选按钮的条目/值保存到本地sqlite并显示它。提前致谢

1 个答案:

答案 0 :(得分:0)

此代码有效:

function PopulateMDB(tx)
{
    tx.executeSql('CREATE TABLE IF NOT EXISTS medicalshop (medicalshop_rating TEXT)');

    var j;  
    for (j = 0; j < 5; j++)
    {
        var store = document.forms[j];
        var i;
        var txt = "";
        for (i = 0; i < store.length; i++) 
        {
            if (store[i].checked) 
            {
                txt = store[i].value + " ";
                alert(txt);
            }
        }
        tx.executeSql('INSERT INTO medicalshop VALUES(?)',[txt]);
    }
}