为另一个HTML类设置选定的选项

时间:2014-08-11 18:05:54

标签: javascript jquery html css

如何在加载该页面之前为其他页面设置所选选项?

说我有A页和B页。
如果用户单击页面A上的按钮,它会将页面B中的默认选项更改为"某些内容"然后它将使用户转到页面B.(所以我希望它在页面B加载之前改变)。

2 个答案:

答案 0 :(得分:0)

如果你开始使用没有后端语言的纯JS HTML,你可以尝试使用GET变量。

Page 1 HTML:

<a href="p2.html?v=1">Option 1</a><br />
<a href="p2.html?v=2">Option 2</a><br />

P2 HTML:

<form>
  <input id="Option1" type="radio" name="Option" value="1">Option 1 </input>
  <input id="Option2" type="radio" name="Option" value="2">Option 2 </input>
</form>

Page 2 JS:

var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})  //Parse GET Params
document.getElementById("Option" + queryDict["v"]).checked = true;  //Set Option checked

答案 1 :(得分:0)

我会使用JavaScript的localStorage()对象。所以,在第A页上你会有类似的东西:

$(document).ready(function(){
    $('a.PageALink').click(function(){
        localStorage.setItem("PageAValue", $(this).text()); // Or use the data attribute, if you want the stored value to be something else, e.g. <a data-value="value to store" href="somewebsite.com">link text</a>
});

然后在第B页:

$(document).ready(function(){
    $('a.PageBLink').text(localStorage.PageAValue);
});

当然,这个答案假定您的页面已经包含jQuery或者您知道如何包含它。如果这不适合您,请告诉我们您在依赖关系和/或脚本方面的限制。