我环顾四周,没有找到任何有用的方法,如果这是一个重复的问题,那就很抱歉。
如果用户点击某个链接,我会尝试将其发送到预先选中了复选框的页面。
例如,我有3个类别
PSHE,Food和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-->
答案 0 :(得分:0)
是的哈利,你的任命是正确的。而不是将复选框值分配给位置哈希。写条件语句以指定所需的URL本身打开
$('input[type=checkbox]').click(function() {
var x = $(this).val();
if (x == "cond 1") {
window.open("url1", "_self");
} else if (x == "cond2") {
window.open("url2", "_self"); //and so on
}
});
希望有所帮助......