【问题标题】:Pre-fill the checkbox with url用 url 预先填写复选框
【发布时间】:2021-01-01 03:53:36
【问题描述】:

我知道我们可以使用 URL 在 HTML 表单中预先填写一些数据。 例如www.example.com/?itemname=sth

我可以知道是否有类似的方法可以通过 URL 预先选中复选框?

<ul class="list-group">
   <li class="list-group-item">
       <div class="form-check">
          <label class="form-check-label">
            <input type="checkbox" class="form-check-input product_check" id="Tutor_subject_1" name="Maths" value="Maths" 
<?php if (isset($_GET['itemname']) && $_GET['itemname'] === 'sth' ): ?> 
           document.getElementByName("Maths").click();
       <?php endif; ?>>Maths<br>
          </label>
        </div>
    </li>
</ul>

谢谢@jrswgtr。我还有一个后续问题。上面的设置其实就是我的结果页的过滤器。您的解决方案是可行的,但它只是选中了该框,但没有像通常那样将所选项目的值传递给我的 SQL。请问怎么改?

【问题讨论】:

    标签: php html url checkbox


    【解决方案1】:

    可以,请检查$_GET 参数:

    <input type="checkbox"
           class="form-check-input product_check"
           id="Tutor_subject_1"
           value="Maths"
           <?php if (isset($_GET['itemname']) && $_GET['itemname'] === 'sth' ): ?> 
               checked
           <?php endif; ?> />
    

    【讨论】:

    • 谢谢@jrswgtr。我还有一个后续问题。上面的设置其实就是我的结果页的过滤器。您的解决方案是可行的,但它只是选中了该框,但没有像通常那样将所选项目的值传递给我的 SQL。请问怎么改?
    • @sevenine 我不确定您所说的 SQL 部分。请在代码中包含您对问题执行 SQL 操作的部分
    • 嗨@jrswgtr,我意识到我的代码是通过单击复选框触发的。我已将“checked”更改为“document.getElementByName("Maths").click()”并添加了一个 name="Maths",但事实证明它不起作用。
    • 请将更改后的代码添加到您的问题中,仅此评论太模糊,无法帮助您。
    • 对此我很抱歉。我已经做出了相应的改变。我想要的是那个 URL,如果一切都匹配,然后单击复选框。感谢您的时间@jrswgtr
    【解决方案2】:

    你可以使用 php 来解决这个问题。

    <?php
    $item =  (isset($_GET['itemname']) && $_GET['itemname'] === 'sth') ? $_GET['itemname'] : '';
    ?>
    
    <ul class="list-group">
       <li class="list-group-item">
           <div class="form-check">
              <label class="form-check-label">
                <input type="checkbox" class="form-check-input product_check" id="Tutor_subject_1" value="Maths" <?php if($item) {echo "checked";} ?> >Maths<br>
              </label>
            </div>
        </li>
    </ul> 
    

    【讨论】:

    • 谢谢@ahmed。我还有一个后续问题。上面的设置其实就是我的结果页的过滤器。您的解决方案是可行的,但它只是选中了该框,但没有像通常那样将所选项目的值传递给我的 SQL。请问怎么改?
    猜你喜欢
    • 2014-12-11
    • 2020-10-21
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多