admin管理员组文章数量:1434014
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
2 Answers
Reset to default 6Don'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
本文标签: javascripthtml buttons not workingStack Overflow
版权声明:本文标题:javascript - html buttons not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745611631a2666144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论