【问题标题】:Getting a URL to decide value of a checkbox获取一个 URL 来决定一个复选框的值
【发布时间】: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


【解决方案1】:

是的,哈利,你的方法是正确的。而不是将复选框值分配给位置哈希。编写条件语句来分配所需的 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
    }
});

希望对你有帮助。

【讨论】:

  • 感谢您的回复!你能帮我看看我到底需要什么,再次感谢。 :)
  • 例如:if (x == ".category-what-food") { window.open("correspondingurl", "_self"); }else if (x == ".category-what-pshe") { window.open("correspondingurl", "_self"); } // 以此类推,如果您是动态生成 url,也可以将值作为 #param 附加到 url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 2017-11-22
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多