javascript - html buttons not working - Stack Overflow

I am trying to get 2 buttons to work. One should save my local storage and the other should read my loc

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611631a4635979.html

相关推荐

  • javascript - html buttons not working - Stack Overflow

    I am trying to get 2 buttons to work. One should save my local storage and the other should read my loc

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信