从HTML5中删除数据库中的记录

时间:2012-10-16 11:10:26

标签: database html5 indexeddb delete-row

使用HTML5代码,即使触发了onsuccess事件,我也无法从数据库中删除。我的删除代码如下:

btnDelete.addEventListener("click", function(){
        //alert("At Delete function");
        var AadharNumber = document.getElementById("AadharNumber").value;
        var transaction = db.transaction("CustDetails",IDBTransaction.READ_WRITE);
        var objectStore = transaction.objectStore("CustDetails");
        var request = objectStore.delete(AadharNumber);
        request.onsuccess = function(event){
            alert( "Aadhar Number: "+ AadharNumber + " deleted from the database");
        };

    },false);

然后当我在删除后单击打印按钮时,我仍然可以看到相同的记录。这是我的打印功能。

btnPrint.addEventListener("click", function () {
                    var output = document.getElementById("printOutput");
                    output.textContent = "";
                    var transaction = db.transaction("CustDetails", IDBTransaction.READ_WRITE);
                    var objectStore = transaction.objectStore("CustDetails");

                    var request = objectStore.openCursor();
                    request.onsuccess = function(event) {  
                        var cursor = event.target.result;  
                        if (cursor) {  
                            output.textContent += "<li>""Aadhar Number: " + cursor.key + " is " + cursor.value.name + "</li>";
                            cursor.continue();  

                        }  
                        else {  
                            console.log("No more entries!");  
                        }  
                    };  
                }, false); 

我的HTML代码如下:

<body><center>
<div id="container">
    <label for="txtName">
    Name:
    </label>
    <input type="text" id="txtName" name="txtName" />
    <br />
    <label for="txtEmail">
    Email:
    </label>
    <input type="text" id="txtEmail" name="txtEmail" />
    <br />
    <input type="button" id="btnAdd" value="Add Record" />
    <br />
    <label for="AadharNumber">
    ID:
    </label>
    <input type="text" id="AadharNumber" name="txtAadharNumber" />
    <input type="button" id="btnDelete" value="Delete Record" />
    <br />
    <input type="button" id="btnPrint" value="Print Records" />
    <br />
    <output id="printOutput" > </output>
</div>  
</center>
</body>

我的删除有什么问题?感谢。

1 个答案:

答案 0 :(得分:2)

代码看起来很好但是你可以检查将字符串键转换为整数值是否有助于从对象库中删除对象。

var AadharNumber =  parseInt(document.getElementById("AadharNumber").value);