Javascript字典问题

时间:2013-12-09 02:51:39

标签: javascript dictionary

我正在为使用phonegap创建的照片应用创建基于javascript的“收藏分配”列表。

我将它基于“待办事项列表”教程代码,并尝试根据我的目的进行调整。

有两个变量:标题(文本)和链接(text2)

它在浏览器中运行良好,并且首次在应用程序中启动。 但是在刷新或重新启动时,我不认为它将链接变量保存到字典中。

非常感谢代码的任何指导:

<script type="text/javascript" language="JavaScript">

    //Create a new To-Do
    function createNewToDo()
    {
        var todoDictionary = {};
        //Prompt the user to enter To-Do
        var todo="FAST SHUTTER SPEEDS";
        var todolink="#fastshutter";
        if (todo!=null)
        {
            if (todo == "")
            {
                alert("To-Do can't be empty!");
            }
            else
            {
                //Append the new To-Do with the table
                todoDictionary = { check : 0 , text : todo , text2 : todolink};
                addTableRow(todoDictionary,false);
            }
        }

    }


    //Add a row to the table
    var rowID = 0;
    function addTableRow(todoDictionary, appIsLoading)
    {

        rowID +=1;
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        //Set up the CheckBox
        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "deleteButton";
        element1.value = "X";

        element1.setAttribute("onclick","deleteSelectedRow(this)");
        element1.className = "deleteButton";
        cell1.appendChild(element1);


        //Set up the View Button
        var cell2 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "viewButton";
        element2.value = todoDictionary["text"];
        var link = todoDictionary["text2"];
        element2.id = rowID;
        element2.onclick=function(){ window.location.hash = link; };
        element2.className = "viewButton";
        cell2.appendChild(element2);

        //Save & Update UI
        saveToDoList();

        if (!appIsLoading)
        alert("Assignment Added To Favourite List!");
    }




    //Deletes current row
    function deleteSelectedRow(deleteButton)
    {
        var p=deleteButton.parentNode.parentNode;
        p.parentNode.removeChild(p);
        saveToDoList();
    }




    function saveToDoList()
    {
        //Create a todoArray
        var todoArray = {};
        var checkBoxState = 0;
        var textValue = "";
        var text2Value = "";

        //Get current table
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;

        if (rowCount != 0)
        {
            //Loop through all rows
            for(var i=0; i<rowCount; i++)
            {
                var row = table.rows[i];

                //Add checkbox state
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked)
                {
                    checkBoxState = 1;
                }
                else
                {
                    checkBoxState= 0;
                }


                //Add text data
                var textbox = row.cells[1].childNodes[0];
                textValue = textbox.value;


                //Fill the array with check & text data
                todoArray["row"+i] =
                {
                    check : checkBoxState,
                    text : textValue
                };

            }
        }
        else
        {
            todoArray = null;
        }

        //Use local storage to persist data
        //We use JSON to preserve objects

        window.localStorage.setItem("todoList", JSON.stringify(todoArray));
    }



    function loadToDoList()
    {

        //Get the saved To-Do list array by JSON parsing localStorage
        var theList = JSON.parse(window.localStorage.getItem("todoList"));


        if (null == theList || theList=="null")
        {
            deleteAllRows();
        }
        else
        {
            var count = 0;
            for (var obj in theList)
            {
                count++;
            }

            //Clear table
            deleteAllRows();
            //Loop through all rows
            for(var i=0; i<count; i++)
            {
                //Add row
                addTableRow(theList["row"+i],true);
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

saveToDoList函数中,您似乎没有将text2值添加到保存到localStorage的对象数组中:

todoArray["row"+i] =
{
      check : checkBoxState,
      text : textValue
};

应该是:

todoArray["row"+i] =
{
      check : checkBoxState,
      text : textValue,
      link : // get the value of your link
};

由于您尚未发布关联的HTML,因此我无法告诉您如何获取链接的值