HTML多重选择值作为GET变量中的逗号分隔字符串 [英] HTML Multiple Select value as comma separated string in GET variable

查看:74
本文介绍了HTML多重选择值作为GET变量中的逗号分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个多选列表,如下所示:

I have set up a multi-select list as follows:

<select id="cuisine-select" name="_sft_post_tag" multiple="multiple" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
    <option value="banting">Banting</option>
    <option value="pasta">Pasta</option>
    <option value="pizza">Pizza</option>
    <option value="seafood">Seafood</option>
    <option value="vegan">Vegan</option>
    <option value="vegetarian">Vegetarian</option>
</select>

这允许用户选择多种美食进行搜索.

This allows the user to select multiple cuisines for their search.

提交表单后,URL会设置多个具有相同名称的GET变量,每个变量都具有选定的值之一,如下所示:

When the form is submitted, the URL sets multiple GET variables of the same name each with one of the selected values, like so:

/restaurant/?_sft_category=saturday&_sft_post_tag=pizza&_sft_post_tag=seafood

我要求它在结果页面上预先选择标签选项.

I require this to pre-select tag options on the results page.

我目前遇到的问题是URL的结构.我要求该URL如下所示:

Where I am currently stuck is the structure of the URL. I require the URL to look as follows:

/restaurant/?_sft_category=saturday&_sft_post_tag=pizza,seafood

换句话说,我需要一个一个 GET变量,即 _sft_post_tag ,并且所有选定的选项都必须以逗号分隔的字符串形式出现.

In other words, I require one GET variable namely _sft_post_tag and all selected options must appear in a comma separated string.

搜索"表单是一个名为Search&的Wordpress插件的一部分.筛选Pro,并使用逗号分隔的字符串预选选项.主页上的搜索表单使用Select2插件.

The Search form is part of a Wordpress plugin called Search & Filter Pro and uses comma separated strings to pre-select options. The search form from the homepage uses the Select2 plugin.

我尝试过使用 name ="_ sft_post_tag []" ,然后将数组内嵌到以逗号分隔的字符串中,例如 implode(,",$ _GET ['_ sft_post_tag']).

I have tried using name="_sft_post_tag[]" and then imploding the array into a comma separated string like so implode(",", $_GET['_sft_post_tag']).

这也不起作用.目前,它只会预填充 _sft_post_tag

This also didn't work. At the moment it'll only pre-populate the last value of the _sft_post_tag

推荐答案

最终,我的回答很简单:

Eventually my answer was as simple as:

$('#form-specials').on('submit', function(e) {
        e.preventDefault();

        var cuisines = $('#cuisine-select').val().join(',');
        var day = $('#day-select').val();

        window.location = 'restaurant?_sft_category=' + day + "&_sft_post_tag=" + cuisines;
    });

这篇关于HTML多重选择值作为GET变量中的逗号分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆