Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为另一个HTML类设置所选选项_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 为另一个HTML类设置所选选项

Javascript 为另一个HTML类设置所选选项,javascript,jquery,html,css,Javascript,Jquery,Html,Css,在加载另一页之前,如何设置该页的选定选项 假设我有A页和B页。 如果用户单击页面a上的按钮,它会将页面B中的默认选项更改为“某物”,然后将用户带到页面B。(因此,我希望在加载页面B之前将其更改)。如果您设置为使用纯JS HTML,而不使用后端语言,则可以尝试使用GET变量 第1页HTML: <a href="p2.html?v=1">Option 1</a><br /> <a href="p2.html?v=2">Option 2</a>

在加载另一页之前,如何设置该页的选定选项

假设我有A页和B页。

如果用户单击页面a上的按钮,它会将页面B中的默认选项更改为“某物”,然后将用户带到页面B。(因此,我希望在加载页面B之前将其更改)。

如果您设置为使用纯JS HTML,而不使用后端语言,则可以尝试使用GET变量

第1页HTML:

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

为此,我将使用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>
});

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

您使用的是后端语言吗?发布您目前正在使用的代码,在加载页面之前运行脚本-是的,我确实可以访问PHP。
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
$(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>
});
$(document).ready(function(){
    $('a.PageBLink').text(localStorage.PageAValue);
});