0

我想要实现的是在 cookie 中存储 2 个数组,例如:["reminder1","reminder2"]``["time1","time2"]

两个 ` 字符用于分隔两个数组。

使用我拥有的当前代码,cookie 的值只是 ,仅reminder1,time1此而已。

我真的在这里做错了什么,但在这一点上,我不知道如何解决这个问题。

到目前为止我的代码:

标记:

<table id="reminders">
    <thead>
        <tr>
            <td>Emlékeztető szövege</td>
            <td>Időpont</td>
            <td>Műveletek</td>
        <tr>
    </thead>
    <tbody>
        <tr class="remdef">
            <td class="remtxt"><em>Kattints a módosításhoz!</em></td>
            <td class="remtim"><input type="text" class="datepicker"></td>
            <td class="remope" style="opacity:1.0;"></td>
        <tr>
    </tbody>
</table>

脚本:

var addnew_html = '<span class="typicn plus '+readCookie('nev')+'" onclick="remtbl(\'addnew\')"></span>';
var modify_html = '<span class="typicn edit '+readCookie('nev')+'" onclick="remtbl(\'modify\')"></span>';
var remove_html = '<span class="typicn times '+readCookie('nev')+'"  onclick="remtbl(\'remove\')"></span>';

$('#reminders tbody tr.remdef td.remtxt em').click(function(){
    defhtml = '<em>'+$(this).html()+'</em>';
    $(this.parentNode).html('<textarea width="100%" cols="50" id="rem-editing" class="rem-edit'+$('#reminders tbody tr').index($(this).parents('#reminders tbody tr'))+'"></textarea>');
    changeModifOptions($('#reminders tbody tr.remdef td.remope'),['addnew']);
});

function changeModifOptions(selector,options){
    $(selector).html(function(){
        return  ((!(options.indexOf('addnew'))) ? addnew_html : '' )+((!(options.indexOf('modify'))) ? modify_html : '')+((!(options.indexOf('remove'))) ? remove_html : '');
    });
}

function remtbl(cmd){
    if (cmd == 'addnew'){
        var cookieval = readCookie('reminder');
        createCookie('reminder',($('#reminders tbody tr.remdef td.remtxt textarea').val().replace('<','&lt;').replace('>','&gt;')+','+$('#reminders tbody tr.remdef td.remtim input.datepicker').val()+( (cookieval) ? '``'+cookieval : '')),parent.longtime);
        $('#reminders tbody').append('<tr class="remelm"><td class="remtxt">'+Array(readCookie('reminder').split('``'))[0]+'</td><td class="remtim"><input type="text" class="datepicker" value="'+Array(readCookie('reminder').split('``'))[1]+'"></td><td class="remope" style="opacity:1.0;"></td><tr>')
        changeModifOptions($('#reminders tbody tr td.remope:last'),['modify','remove']);
        $('#reminders tbody tr.remdef td.remtim input.datepicker').val('');
        $('#reminders tbody tr.remdef td.remope').html('');
    }
}
4

2 回答 2

1

javascript string.split 的第一个参数采用字符或正则表达式。在您发布的代码中,您都没有做这些。

http://www.w3schools.com/jsref/jsref_split.asp

要使其成为有效的正则表达式,请使用 slash backtic 反斜杠。像这样:

/``/
于 2012-12-15T22:23:19.797 回答
1

正如@Andreas 建议的那样,您可以使用将数组转换为 JSON 字符串并使用JSON.stringify( array)将其返回到 javascript 数组JSON.parse( string)

例子

var arr=['a','b','c'];
var json=JSON.stringify(arr);

createCookie('reminder', json);

var arrayFromCookie= JSON.parse( readCookie('reminder'));

对于不支持 JSON 对象的旧浏览器,请包含 json.js 库

于 2012-12-15T22:55:36.813 回答