将复选框状态从一个页面传递到另一个页面
问题描述:
我有2个网页,页面A和页面B,页面A有链接(链接1和链接2),页面B有复选框(复选框1和复选框2)。当我点击页面A上的链接1时,它将更改为页面B,页面B中的复选框1将被反过来检查。主要用于HTML5和/或jQuery的将复选框状态从一个页面传递到另一个页面
感谢
不忘展现例如编码.. 这就是我试图
页A
<a href="pageb.html#check1" checked>Link to check Checkbox1</a>
<a href="pageb.html#check2" checked > Link to Check Checkbox2</a>
网页B
<input type="checkbox" id="checkbox1">
<Label for="checkbox1>CheckBox1</label>
<input type="checkbox" id="checkbox2">
<Label for="checkbox2>CheckBox2</label>
答
如果您可以使用html5,localStorage可能会对您有所帮助。只需保存你的复选框的状态在第A与localStorage.setItem(“pageAcheck”,“真”),然后在用localStorage.get项目(“页面中检查”)
第二页得到它网页A
<input id="cA" type="checkbox" />A
$("#cA").change(function (event) {
alert($("#cA").is(":checked"));
localStorage.setItem("cA",$("#cA").is(":checked"));
});
网页B
<input id="cAB" type="checkbox" />B
// in the second page on load
$(function() {
if (localStorage.getItem("cA") == "true")
$("#cB").prop("checked","true")
});
答
如果你想用jQuery的结合[localStorage][1]
你可以这样做:
在将pageA.html:
<script>
$(function() {
$('input[name=check]').on('change', function() {
window.localStorage.setItem('pageACheck', $(this).is(':checked'));
});
});
</script>
<input type='checkbox' name='check'/> Check
然后在PageB.html,你可以有这样的代码:
<script>
$(function() {
$('input[name=check]').attr("checked", window.localStorage.getItem('pageACheck'));
});
</script>
<input type='checkbox' name='check'/> Check
什么PageA.html
代码的作用是有约束力的复选框来更改事件一个存储更改值的回调函数。然后,在DOM Ready回调中,PageB.html
中的代码正在获取存储的值并设置该复选框,实际上是在页面之间传递值。
+0
两者都使用复选框输入?但我正在寻找的是页面只有链接,换句话说,使用链接来检查其他页面上的复选框 – trasyaari
你可以使用jQuery来阅读和设置复选框,但它好像你只需要[Window.localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage ),它[非常支持](http://caniuse.com/#search=localstorage) –