在我的网站上,用户可以按流派过滤游戏.当用户从选择框中选择一种类型并点击提交时,页面使用 GET 来查看过滤器是什么.现在过滤器工作正常,问题是选择框的选择变为默认选择(即全部".)
On my website, user's are allowed to filter games by Genre. When a user chooses a genre from the select box and hits submit, the page uses GET to see what the filter was. Now the filter works fine, the problem is that the select box's selection goes to the default one (Which says "All".)
我希望在用户提交过滤请求后,选择框将在页面重新加载后保留该选择.
I want it so that after a user submits their filter request, the select box will keep that selection after the page reloads.
我只能想到一种方法来做到这一点,但它需要将 PHP 添加到每个选项中.有没有更简单的方法可以用 PHP 或 jQuery 来做到这一点?
There's only one way I could think of to do this but it would require adding in PHP into every option there is. Are there any simpler ways to go about doing this with PHP or jQuery?
假设你正在做一个完整的表单提交,选择的选项只会对服务器端代码可用,一旦它返回给客户端使用 jQuery 你不会有那个(除非你在表单提交之前尝试使用 cookie,但是 bleh).
Assuming you're doing a full form submit the selected option is only going to be available to the server-side code, once it gets back to the client to use jQuery you won't have that (unless you try to use cookies before the form submit, but bleh).
我会在选项标签中使用 PHP,如果选项与所选选项匹配,则回显 selected="selected".
I'd use PHP in the option tag and echo selected="selected" if the option matches up with the selected option.
如果你想避免大量重复的代码,为什么不做这样的事情:
If you want to avoid a lot of duplicated code why not do something like this:
<select name="test">
<?php
$options = array(1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3');
foreach ($options as $key => $value) {
echo '<option value="' . $key . '"' . ($key == $_GET["test"] ? ' selected="selected"' : '') . '>' . $value . '</option>';
} ?>
</select>
这篇关于提交后保持选中的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)