在我正在工作的joomla网站上,我有一个页面,可以选择注册其中一个英语语言课程。用户通过单击所选按钮来选择,该按钮链接到所选节目的注册页面。链接的html是:
<a href="/index.php/register" id="Starting English" class="enrol_btn">Enrol/ลงทะเบียน</a>
.....
<a href="/index.php/register" id="Senior School" class="enrol_btn">Enrol/ลงทะเบียน</a> and so on
我想保存id值,以便在显示时可以将其插入相应的注册页面。作为第一步,我使用了localStorage和jQuery来保存所需的id值,如下所示:
jQuery(document).ready(function () {
localStorage.prog = jQuery('a.enrol_btn').attr('id');
+`enter code here` alert(localStorage.prog);
});
这不起作用,我不知道为什么。 Firebug显示正在加载包含此代码的js文件。任何帮助将不胜感激。
答案 0 :(得分:1)
如果您尝试将值设置为localstorage(即ID),则可以在同一个域上访问它,但可以在不同的页面上访问它。你必须做这样的事情:
localStorage.setItem('id', jQuery('a.enrol_btn').attr('id'))
稍后,使用以下方式获取该项目:
localStorage.getItem('id')
答案 1 :(得分:0)
我想告诉你,你的锚点的ID值设置不正确, ,因为我可以在其中看到空格,这绝对无效 。
最好使用_
下划线填充空格:
<a href="/index.php/register" id="Starting_English" class="enrol_btn">Enrol/ลงทะเบียน</a>
.....<!--check this--------^^^^^^^^^^^^^^^^^^up and dowwn-->
<a href="/index.php/register" id="Senior_School" class="enrol_btn">Enrol/ลงทะเบียน</a>
您需要在页面加载时使用if/else
条件:
jQuery(document).ready(function () {
var ls = window.localStorage;
if(window.location.href.indexOf('register') != 0){
var reg = ls.getItem('registerPage');
console.log(reg);
}
// but you need to make sure you add an obj in the localStorage when you click the link
$('a').click(function(e){
e.preventDefault();
ls.setItem('registerPage', this.id); // set the id to localstorage here
window.location.href = $(this).attr('href');
});
});