最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - html buttons not working - Stack Overflow

matteradmin8PV0评论

I am trying to get 2 buttons to work. One should save my local storage and the other should read my local storage. For some reason they still aren't working. I did have the alert working, but now that doesn't work. For reading the local storage I would like to use the jQuery append() method. Any thoughts on what I'm missing.

script

$(document).ready(function() {


   function saveLocal(){
if (window.localStorage) {
    localStorage.setItem("firstname","myfirstname");
    localStorage.setItem("lastname","mylastname");
    localStorage.setItem("state","mystate");
    alert("The data has been saved locally.");
} else {
    alert("Your Browser does not support LocalStorage.");
}
            }

     function readLocal(){
if (window.localStorage) {
    var firstname = localStorage.getItem("myfirstname");
    var lastname = localStorage.getItem("mylastname");
    var coursetitle = localStorage.getItem("mystate");

     $("#message").empty().append( firstname + " " + lastname + "  "  +  state                     );
}else {
    alert("Your Browser does not support LocalStorage.");
}
         }


      }); // end ready

html

       </div>
       </p>
       <p>

   <div id="main">
<input type="button" value="Save Values" onclick="saveLocal()"/>
<input type="button" value="Read Values" onclick="readLocal()"/>
        </div>

I am trying to get 2 buttons to work. One should save my local storage and the other should read my local storage. For some reason they still aren't working. I did have the alert working, but now that doesn't work. For reading the local storage I would like to use the jQuery append() method. Any thoughts on what I'm missing.

script

$(document).ready(function() {


   function saveLocal(){
if (window.localStorage) {
    localStorage.setItem("firstname","myfirstname");
    localStorage.setItem("lastname","mylastname");
    localStorage.setItem("state","mystate");
    alert("The data has been saved locally.");
} else {
    alert("Your Browser does not support LocalStorage.");
}
            }

     function readLocal(){
if (window.localStorage) {
    var firstname = localStorage.getItem("myfirstname");
    var lastname = localStorage.getItem("mylastname");
    var coursetitle = localStorage.getItem("mystate");

     $("#message").empty().append( firstname + " " + lastname + "  "  +  state                     );
}else {
    alert("Your Browser does not support LocalStorage.");
}
         }


      }); // end ready

html

       </div>
       </p>
       <p>

   <div id="main">
<input type="button" value="Save Values" onclick="saveLocal()"/>
<input type="button" value="Read Values" onclick="readLocal()"/>
        </div>
Share Improve this question edited Jun 1, 2013 at 1:55 Isaac 11.8k5 gold badges35 silver badges45 bronze badges asked Jun 1, 2013 at 1:46 user2197436user2197436 651 gold badge2 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Don't define the functions inside the $(document).ready() handler. The names are only accessible inside that function's scope, so they can't be used in inline handlers, which look up the names in the global scope.

The only thing that should be inside that handler are direct actions that need to take place after the document is loaded. Function definitions do not need to be delayed like that.

Alternatively, instead of using onclick attributes in the HTML, you can use jQuery binding:

$("#savebutton").click(savelocal);
$("#readbutton").click(readlocal);

remove the document ready theres no need for it. your not asking it to run anything. put the script in the head tag and call it a day

Post a comment

comment list (0)

  1. No comments so far