获取一个URL来决定复选框的值


Getting a URL to decide value of a checkbox

我已经环顾四周,没有发现任何太有用的东西,我怎么能做到这一点,所以很抱歉,如果这是一个重复的问题。

我试图得到它,所以如果用户点击某个链接,他们将被发送到一个预先选中的复选框的页面。

例如,我有3个类别

PSHE, Food and 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-->

是的,Harry,你的方法是正确的。而不是将复选框值分配给位置散列。编写条件语句,为打开

指定所需的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
    }
});

希望它能帮助…