Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript字典问题_Javascript_Dictionary - Fatal编程技术网

Javascript字典问题

Javascript字典问题,javascript,dictionary,Javascript,Dictionary,我正在为我使用phonegap创建的照片应用程序创建一个基于javascript的“最喜爱的作业”列表 我基于一个“待办事项列表”的教程代码,并试图使其适应我的目的 有两个变量:标题(文本)和链接(文本2) 它在浏览器中工作得很好,并且在应用程序首次启动时工作得很好。 但是在刷新或重新启动时,我不认为它会将link变量保存到字典中 如有任何关于本准则的指导意见,将不胜感激: <script type="text/javascript" language="JavaScript">

我正在为我使用phonegap创建的照片应用程序创建一个基于javascript的“最喜爱的作业”列表

我基于一个“待办事项列表”的教程代码,并试图使其适应我的目的

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

它在浏览器中工作得很好,并且在应用程序首次启动时工作得很好。 但是在刷新或重新启动时,我不认为它会将link变量保存到字典中

如有任何关于本准则的指导意见,将不胜感激:

<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);
            }

        }

    }

//创建一个新的任务
函数createNewToDo()
{
var toddictionary={};
//提示用户输入要执行的操作
var todo=“快速快门速度”;
var todolink=“#快速快门”;
如果(todo!=null)
{
如果(todo==“”)
{
提醒(“待办事项不能为空!”);
}
其他的
{
//附加新的属性以处理表
todoDictionary={检查:0,文本:todo,文本2:todolink};
addTableRow(ToDictionary,false);
}
}
}
//向表中添加一行
var-rowID=0;
函数addTableRow(ToDictionary,appIsLoading)
{
rowID+=1;
var table=document.getElementById(“数据表”);
var rowCount=table.rows.length;
var row=table.insertRow(rowCount);
//设置复选框
var cell1=行插入单元格(0);
var element1=document.createElement(“输入”);
element1.type=“deleteButton”;
元素1.value=“X”;
element1.setAttribute(“onclick”、“deleteSelectedRow(this)”);
element1.className=“deleteButton”;
单元格1.附加子元素(元素1);
//设置视图按钮
var cell2=行插入单元格(1);
var element2=document.createElement(“输入”);
element2.type=“查看按钮”;
element2.value=toddictionary[“text”];
var link=toddictionary[“text2”];
element2.id=rowID;
element2.onclick=function(){window.location.hash=link;};
element2.className=“viewButton”;
第2单元。附加子元素(元素2);
//保存和更新用户界面
saveToDoList();
如果(!appIsLoading)
警报(“作业已添加到收藏夹列表!”);
}
//删除当前行
函数deleteSelectedRow(删除按钮)
{
var p=deleteButton.parentNode.parentNode;
p、 parentNode.removeChild(p);
saveToDoList();
}
函数saveToDoList()
{
//创建一个todoArray
var todoArray={};
var checkBoxState=0;
var textValue=“”;
var text2Value=“”;
//获取当前表
var table=document.getElementById(“数据表”);
var rowCount=table.rows.length;
如果(行计数!=0)
{
//循环遍历所有行

对于(var i=0;i而言,在
saveToDoList
函数中,似乎没有将text2值添加到保存到本地存储的对象数组中:

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

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

由于您尚未发布关联的HTML,我无法告诉您如何在重新启动应用程序时获取链接的值,这可能会清除为上一个实例分配的本地存储。至于在浏览器中刷新,您认为它没有保存的原因是什么?您是否运行了
window.localStorage.getItem(“todoList”)
在浏览器的JS控制台中?它不是将其清除,而是在下次应用程序加载正确的标题和喜爱的工作分配号码时重新加载列表。phonegap将其从本地存储传输到iphone的永久存储。它保存标题(文本),但不保存链接(text2)。只要我不刷新整个index.html(这是一个带有单独div的大型html文档),链接就会正常工作,然后它会显示“未定义”或者只要应用程序保持活动状态……重新启动时,链接就不起作用。保存前,您是否验证了
textbox
textbox.value
是否为有效值?如果是,示例值是什么?我相信是的。如果我将“text2”作为标题,则链接(#fastshutter.html)显示为标题名。我基于未修改的示例代码非常完美,但我添加了第二个文本元素(链接),这是主要区别。在
text:textValue