当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - on page load javascript check value from HTML5 webstorage table's field

问题描述:

When my page loads, I create a database with fields and data is successfully loaded in all the fields.

In my webstorage database their is a column favourite and its type is integer. when my database is filling data in all fields this, the favourite field is field with 0 values.

After this there I display only one button in my page whose code is:

<img id="fav_image" onclick="addFav()" src="icons/star_fav_empty_icon.png" />

When I click this image, it changes the value from 0 to 1 in the favourite field in database.

As you see on id 3 favourite value is 1 in abc table

After changing image value, its function is also changed ...

... which I'm doing like this:

function addFav()

{

var pageId = a;

db.transaction(function(tx) {

tx.executeSql(updateFav,[1,pageId], showEmpty , onError);

});

document.getElementById('fav_image').src="icons/star_fav_icon_fill.png";

document.getElementById('fav_image').setAttribute( "onClick", "javascript:

removeFav();" );

}

function removeFav()

{

var pageId = a;

db.transaction(function(tx) {

tx.executeSql(updateFav,[0,pageId], showEmpty , onError);

});

document.getElementById('fav_image').src="icons/star_fav_empty_icon.png";

document.getElementById('fav_image').setAttribute( "onClick", "javascript: addFav();"

);

}

According to me, everything is working fine here.

I need to check the favourite value when page is loaded and show the image and onClick function according to the favourite value:

  • if the value is 0, then show empty star image with the addFav() function
  • if the value is 1, then show filled star image with the RemoveFav() function

I get the favourite value from the database using this function:

function checkfav()

{

var cID = a;

var b;

var getFavBool = 0;

//fav.innerHTML = '';

db.transaction(function(tx) {

tx.executeSql(selectfavStatement, [cID], function(tx, result) {

dataset = result.rows.item(0);

getFavBool = dataset['favourite'];

b = parseInt(getFavBool,10);

if(b==1)

{

fav.innerHTML = b;

return b;

}

else

{

fav.innerHTML = b;

return b;

}

});

});

}

I hope you get the idea of what I am trying to say...

网友答案:

I only added the checkfav() function whose code is:

function checkfav()
{
    //this is a ID to which i want to get the result.
    var cID = a;    
   db.transaction(function(tx) {
   tx.executeSql("selectfavStatement", [cID], function (tx, result) {
   var len = result.rows.length, i;
   dataset = result.rows;
   for (var i = 0, item = null; i < len; i++){
    item = dataset.item(i); 
        var favID = item['favourite'];

        //now this line is only to show result on HTML page. (optional)
        document.getElementById('fav').innerHTML = item['favourite']; 
        if(favID==1)
        {
           document.getElementById('fav_image').src="icons/star_fav_icon_fill.png";
           document.getElementById('fav_image').setAttribute( "onClick", "javascript: addFav();"          );    
        }
        else if(favID==0)
        {
            document.getElementById('fav_image').src="icons/star_fav_empty_icon.png";
            document.getElementById('fav_image').setAttribute( "onClick", "javascript: removeFav();" );
        }
   }
 });
});

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: