cookie字符串获取并设置jquery控件的属性



有人能帮我完成两个需要的函数吗。我想要一个函数从cookie字符串中获取与控件id匹配的属性值,另一个函数为控件id设置属性值。我的字符串看起来像这个

var cookieValue='id=1&state=normal&theme=purple:id=2&state=maximized&theme=pink';

字符串必须使用冒号cookieValue.split(":")对每个控件属性进行拆分;当设置propertyValue时,cookieString必须更新并连接在一起,有人知道如何做到这一点吗。

功能看起来像这个

function setPropertyValue(cookieString, id,  propertyName, propertyValue) {
    if(id) setProperty_Value
    return cookieString;
}
function getPropertyValue(cookieString, id, proprtyName) {
     return propertyValue;
}

试试这个:

function setPropertyValue(cookieString, id,  propertyName, propertyValue) {
  cookieObjects = cookieString.split(':');
  for (var i = 0; i < cookieObjects.length; i++)
  {
    cookieObject = cookieObjects[i];
    if (cookieObject.indexOf("id=" + id) >= 0)
    {
      cookieProperties = cookieObject.split("&");
      for (var j = 0; j < cookieProperties.length; j++)
      {
        cookieProperty = cookieProperties[j];
        if (cookieProperty.indexOf(propertyName) >= 0)
        {
          return cookieString.replace(cookieProperty, cookieProperty.split("=")[0] + "=" + propertyValue);
        }
      }
    }
  }
  return propertyValue;
}
function getPropertyValue(cookieString, id, propertyName) {
  cookieObjects = cookieString.split(':');
  for (var i = 0; i < cookieObjects.length; i++)
  {
    cookieObject = cookieObjects[i];
    if (cookieObject.indexOf("id=" + id) >= 0)
    {
      cookieProperties = cookieObject.split("&");
      for (var j = 0; j < cookieProperties.length; j++)
      {
        cookieProperty = cookieProperties[j];
        if (cookieProperty.indexOf(propertyName) >= 0)
        {
          return cookieProperty.split("=")[1];
        }
      }
    }
  }
  return propertyValue;
}
var cookieValue='id=1&state=normal&theme=purple:id=2&state=maximized&theme=pink';
alert(getPropertyValue(cookieValue, "1", "state"));
cookieValue = setPropertyValue(cookieValue, "1", "state", "not");
alert(cookieValue);

最新更新