【发布时间】:2014-08-06 09:17:10
【问题描述】:
我环顾四周,没有发现任何关于如何做到这一点的有用信息,如果这是一个重复的问题,请见谅。
我正在尝试获取它,因此如果用户单击某个链接,他们将被发送到预先选中复选框的页面。
例如,我有 3 个类别
PSHE、食品和 EHWB。
我有一个页面,其中包含所有带有这些类别的帖子,但过滤器只显示所选类别。
我想在主页上有3个链接,如果点击,加载页面并预先选中相应链接的复选框。
最好的方法是什么?
到目前为止我还没有找到:
$('input[type=checkbox').click(function(){
window.location.hash = $(this).val();
});
单击时将复选框的值添加到 URL,我不确定这是否是一个说明点,但我们将不胜感激。谢谢。
编辑:
这是我要求的 HTML。
<fieldset id="filters" class="form__section form__section--group form__section--categories">
<legend class="form__section__title">
<h2 class="form__section__title__text">Filter by category:</h2>
</legend>
<div class="form__item form__item--boolean form__item--checkbox">
<button type="button" value=".all" id="checkAll">All</button>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-food" value=".category-what-food" id="category-what-food ff-checkbox-1" class="checkbox1" /><label for="category-what-food ff-checkbox-1">Food</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-pshe" value=".category-what-pshe" id="category-what-pshe ff-checkbox-2" class="checkbox1" /><label for="category-what-pshe ff-checkbox-2">PSHE</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-sex-education" value=".category-what-sex-education" id="category-what-sex-education ff-checkbox-3" class="checkbox1" /><label for="category-what-sex-education ff-checkbox-3">Sex Education</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-physical-education" value=".category-what-physical-education" id="category-what-physical-education ff-checkbox-4" class="checkbox1" /><label for="category-what-physical-education ff-checkbox-4">Physical Education</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-ehwb" value=".category-what-ehwb" id="category-what-ehwb ff-checkbox-5" class="checkbox1" /><label for="category-what-ehwb ff-checkbox-5">EHWB</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-environment" value=".category-what-environment" id="category-what-environment ff-checkbox-6" class="checkbox1" /><label for="category-what-environment ff-checkbox-6">Environment</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-policies" value=".category-what-policies" id="category-what-policies ff-checkbox-7" class="checkbox1" /><label for="category-what-policies ff-checkbox-7">Policies</label>
</div><!--/form__item-->
<div class="form__item form__item--boolean form__item--checkbox">
<input type="checkbox" name="ff-checkbox category-what-governors" value=".category-what-governors" id="category-what-governors ff-checkbox-8" class="checkbox1" /><label for="category-what-governors ff-checkbox-8">Governors</label>
</div><!--/form__item-->
</fieldset><!--/form__group-->
【问题讨论】:
-
也向我们展示您的 html
-
为什么不将其作为查询字符串中的数据参数发送?
-
如果您将第一个链接设为:example.com/somepage.php?Food 然后在 somepage.php 上添加条件 if(isset($_GET['Food'])) echo checked
-
$('input[type=checkbox')这里少了什么?? -
@ManishPuri 如果这样也行,你介意告诉我我会怎么做吗?感谢您的回复。
标签: javascript php html wordpress hashchange