Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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_Jquery_Html_Forms - Fatal编程技术网

Javascript 提交时在本地保存表单更改

Javascript 提交时在本地保存表单更改,javascript,jquery,html,forms,Javascript,Jquery,Html,Forms,我有一个元素,我想用它来更改网页的背景。我成功地改变了背景(虽然是以一种非常不雅的方式),我想知道是否有一种简单的方法来“保存”这个,这样每当用户离开并返回网页时,背景就是他们选择的 我一直在修补cookies和本地存储来实现这一点,但从来没有得到任何工作 HTML: <select id="settingBackground"> <option name="default">Default</option> <option name=

我有一个
元素,我想用它来更改网页的背景。我成功地改变了背景(虽然是以一种非常不雅的方式),我想知道是否有一种简单的方法来“保存”这个,这样每当用户离开并返回网页时,背景就是他们选择的

我一直在修补cookies和本地存储来实现这一点,但从来没有得到任何工作

HTML:

<select id="settingBackground">
    <option name="default">Default</option>
    <option name="thankshaking">Background 2</option>
</select>
$(document).ready(function () {
    $('#settingBackground').on('change', function () {
        if (this.value == "Default") {
            // Change background
            $('body').css("background", "#1e8cd4");
            $('body').css("color", "#fff");
        } else if (this.value == "Background 2") {
            // Change background
            $('body').css("background", "url('http://i.imgur.com/cV7PKqh.jpg') no-repeat center center fixed");
            $('body').css("background-size", "cover");
            $('body').css("color", "#000");
        }
    });
});

下面是一个用于设置和读取cookie的Jquery插件。您甚至可以定义过期时间。

要从中设置cookie,请执行以下操作:

$(function(){

var background = $.cookie('background') || false;

function changeBackground(background){
    if(background !== false){
        $('body').addClass(background);
        $.cookie('background', background);
    }
}

changeBackground(background);



$('body').on('change', '#settingBackground', function(){
    changeBackground($(this).val());
});


});

下面是一个用于设置和读取cookie的Jquery插件。您甚至可以定义过期时间。

要从中设置cookie,请执行以下操作:

$(function(){

var background = $.cookie('background') || false;

function changeBackground(background){
    if(background !== false){
        $('body').addClass(background);
        $.cookie('background', background);
    }
}

changeBackground(background);



$('body').on('change', '#settingBackground', function(){
    changeBackground($(this).val());
});


});

下面是一个用于设置和读取cookie的Jquery插件。您甚至可以定义过期时间。

要从中设置cookie,请执行以下操作:

$(function(){

var background = $.cookie('background') || false;

function changeBackground(background){
    if(background !== false){
        $('body').addClass(background);
        $.cookie('background', background);
    }
}

changeBackground(background);



$('body').on('change', '#settingBackground', function(){
    changeBackground($(this).val());
});


});

下面是一个用于设置和读取cookie的Jquery插件。您甚至可以定义过期时间。

要从中设置cookie,请执行以下操作:

$(function(){

var background = $.cookie('background') || false;

function changeBackground(background){
    if(background !== false){
        $('body').addClass(background);
        $.cookie('background', background);
    }
}

changeBackground(background);



$('body').on('change', '#settingBackground', function(){
    changeBackground($(this).val());
});


});

您可以按如下方式使用
localStorage

$(document).ready(function () {
  var selectedBG = localStorage.getItem("selectedBg");
  if (selectedBG) switchBg(selectedBG);
  $('#settingBackground').on('change', function () {
    localStorage.setItem("selectedBg", this.value);
    switchBg(this.value);
  });

  function switchBg(bg) {
    if (bg == "Default") {
        // Change background
        $('body').css("background", "#1e8cd4");
        $('body').css("color", "#fff");

    } else if (bg == "Background 2") {
        // Change background
        $('body').css("background", "url('http://i.imgur.com/cV7PKqh.jpg') no-repeat center center fixed");
        $('body').css("background-size", "cover");
        $('body').css("color", "#000");
    }
  }
});


但在使用之前,请确保
localStorage
存在。

您可以按如下方式使用
localStorage

$(document).ready(function () {
  var selectedBG = localStorage.getItem("selectedBg");
  if (selectedBG) switchBg(selectedBG);
  $('#settingBackground').on('change', function () {
    localStorage.setItem("selectedBg", this.value);
    switchBg(this.value);
  });

  function switchBg(bg) {
    if (bg == "Default") {
        // Change background
        $('body').css("background", "#1e8cd4");
        $('body').css("color", "#fff");

    } else if (bg == "Background 2") {
        // Change background
        $('body').css("background", "url('http://i.imgur.com/cV7PKqh.jpg') no-repeat center center fixed");
        $('body').css("background-size", "cover");
        $('body').css("color", "#000");
    }
  }
});


但在使用之前,请确保
localStorage
存在。

您可以按如下方式使用
localStorage

$(document).ready(function () {
  var selectedBG = localStorage.getItem("selectedBg");
  if (selectedBG) switchBg(selectedBG);
  $('#settingBackground').on('change', function () {
    localStorage.setItem("selectedBg", this.value);
    switchBg(this.value);
  });

  function switchBg(bg) {
    if (bg == "Default") {
        // Change background
        $('body').css("background", "#1e8cd4");
        $('body').css("color", "#fff");

    } else if (bg == "Background 2") {
        // Change background
        $('body').css("background", "url('http://i.imgur.com/cV7PKqh.jpg') no-repeat center center fixed");
        $('body').css("background-size", "cover");
        $('body').css("color", "#000");
    }
  }
});


但在使用之前,请确保
localStorage
存在。

您可以按如下方式使用
localStorage

$(document).ready(function () {
  var selectedBG = localStorage.getItem("selectedBg");
  if (selectedBG) switchBg(selectedBG);
  $('#settingBackground').on('change', function () {
    localStorage.setItem("selectedBg", this.value);
    switchBg(this.value);
  });

  function switchBg(bg) {
    if (bg == "Default") {
        // Change background
        $('body').css("background", "#1e8cd4");
        $('body').css("color", "#fff");

    } else if (bg == "Background 2") {
        // Change background
        $('body').css("background", "url('http://i.imgur.com/cV7PKqh.jpg') no-repeat center center fixed");
        $('body').css("background-size", "cover");
        $('body').css("color", "#000");
    }
  }
});


在使用它之前,请确保
localStorage
存在。

旁注:您只需在body或其他元素中添加一个类,并将所有这些微妙的细节以CSS样式显示。cookie应该适用于您的情况。如果您尝试过使用Cookie,请展示您的代码。如果您正在“修补Cookie和本地存储”,请展示您所拥有的。否则,请自己搜索一下,学习如何使用或。@hon2a我没有代码,因为我放弃了它(我昨晚正在编写)。不过看起来是这样的:旁注:您应该只向body或其他元素添加一个类,并将所有这些精细的细节放在CSS样式中。cookie应该适用于您的情况。如果您尝试过使用Cookie,请展示您的代码。如果您正在“修补Cookie和本地存储”,请展示您所拥有的。否则,请自己搜索一下,学习如何使用或。@hon2a我没有代码,因为我放弃了它(我昨晚正在编写)。不过看起来是这样的:旁注:您应该只向body或其他元素添加一个类,并将所有这些精细的细节放在CSS样式中。cookie应该适用于您的情况。如果您尝试过使用Cookie,请展示您的代码。如果您正在“修补Cookie和本地存储”,请展示您所拥有的。否则,请自己搜索一下,学习如何使用或。@hon2a我没有代码,因为我放弃了它(我昨晚正在编写)。不过看起来是这样的:旁注:您应该只向body或其他元素添加一个类,并将所有这些精细的细节放在CSS样式中。cookie应该适用于您的情况。如果您尝试过使用Cookie,请展示您的代码。如果您正在“修补Cookie和本地存储”,请展示您所拥有的。否则,请自己搜索一下,学习如何使用或。@hon2a我没有代码,因为我放弃了它(我昨晚正在编写)。但看起来是这样的: