[英]How can I build a URL query string based on a dropdown selection?
I am trying to create a URL query string dynamically based on several different parameters for an ecommerce site I am working on. 我正在尝试根据正在处理的电子商务网站的几个不同参数动态创建URL查询字符串。 The site is on a SaaS platform, so server side coding/scripting is out of the question.
该站点位于SaaS平台上,因此不需要服务器端编码/脚本。
Requirements: 要求:
Client wants to be able to perform a search based on "finish"—such as Chrome, Bronze, etc and on the "condition" (new/used/refurbished for instance). 客户希望能够基于“完成”(例如Chrome,青铜等)和“条件”(例如,新的/二手的/翻新的)进行搜索。
The filters should be able to be added/removed from the URL string without impacting the other variables (such as ?find=search-term, &category=1234, etc). 应当能够在不影响其他变量(例如?find = search-term,&category = 1234等)的情况下从URL字符串添加/删除过滤器。
Current difficulty(s): 目前的困难:
Right now I cannot figure out a way to contruct the URL string dynamically based on the dropdown choice or how to make the dropdown choice persist after pageload until another option is selected. 现在,我无法找出一种基于下拉选项动态构造URL字符串的方法,或者如何使下拉选项在页面加载后持续存在,直到选择了另一个选项。
URL Example search query structure: http://www.domain.com/search.aspx?find=search-term&category=1234&bath-finish=chrome URL示例搜索查询结构: http : //www.domain.com/search.aspx?find= search-term&category= 1234& bath-finish= chrome
Current markup for example(s): 当前标记例如:
This is the HTML for the dropdown list I am working with so far for just the "Finish" option. 到目前为止,这是我正在使用的下拉列表的HTML,仅用于“完成”选项。
<select id="searchbox" name="URL">
<option>Choose Filter ...</option>
<option value="&bath-finish=Chrome">Chrome</option>
<option value="&bath-finish=Bronze">Bronze</option>
<option value="&bath-finish=Black">Black</option>
<option value="&bath-finish=Brass">Brass</option>
<option value="&bath-finish=Copper">Copper</option>
<option value="&bath-finish=Nickel">Nickel</option>
<option value="&bath-finish=Stainless-Steel">Stainless Steel</option>
<option value="&bath-finish=Gold">Gold</option>
<option value="&bath-finish=Silver">Silver</option>
<option value="&bath-finish=Brown">Brown</option>
<option value="&bath-finish=White">White</option>
<option value="&bath-finish=Almond">Almond</option>
<option value="&bath-finish=Bisquit">Bisquit</option>
<option value="&bath-finish=Blue">Blue</option>
<option value="&bath-finish=Clear">Clear</option>
<option value="&bath-finish=Clear-Glass">Clear Glass</option>
<option value="&bath-finish=Wood">Wood</option>
<option value="&bath-finish=Green">Green</option>
<option value="&bath-finish=Grey">Grey</option>
<option value="&bath-finish=Satin">Satin</option>
<option value="&bath-finish=Oil-Rubbed">Oil Rubbed</option>
</select>
This is the script I had attempted to make so far (which only addresses the single filter): 到目前为止,这是我尝试制作的脚本(仅针对单个过滤器):
<script>
var selectFilter= $('#searchbox').val();
$('#searchbox').on("change", function(e){
document.location.href = 'http://www.domain.com/search.aspx?log=false' + selectFilter;
});
</script>
The main issue that I am having right now is that the script doesn't seem to read the selected option from the dropdown box and i'm not 100% sure why. 我现在遇到的主要问题是该脚本似乎没有从下拉框中读取所选的选项,而且我不确定100%为什么。 Ideally, I'd like to have the URL be very modular so that the selections would have pretty tight control over the URL string.
理想情况下,我希望URL具有高度的模块化,以便选择对URL字符串具有相当严格的控制。
Example: 例:
domain.com/search.aspx?find=search-term&filter=selected-filter-value domain.com/search.aspx?find=search-term&filter=selected-filter-value
Where "selected-filter-value" is based upon the selection in the dropdown box. 其中“ selected-filter-value”是基于下拉框中的选择。
try something like this 尝试这样的事情
$('#searchbox').on("change", function(e){
var selectFilter= $(this).val();
document.location.href = 'http://www.domain.com/search.aspx?log=false' + selectFilter;
});
Hope it helps you, 希望对您有帮助,
<script type="text/javascript">
$(function(){
$('#searchbox').on("change", function(){
var selectFilter= $(this).val();
document.location.href = 'http://www.domain.com/search.aspx?log=false' + selectFilter;
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.