gpt4 book ai didi

javascript - 我如何根据 URL 中的参数填充我的搜索字段

转载 作者:行者123 更新时间:2023-11-30 11:28:54 25 4
gpt4 key购买 nike

我正在开发一个 asp.net web 应用程序,我有以下代码来构建搜索字段部分,它根据用户的输入构建 URL 参数:-

<script type="text/javascript">

$(document).ready(function(){

$('#button').click(function(e) {
var count=1;
var s="";

var inputvalue = $("#journal").val();
var inputvalue2 = $("#keywords").val();
var inputvalue3 = $("#datepub").val();
var inputvalue4 = $("#title").val();
var inputvalue5 = $("#localcurrency").val();
var inputvalue6 = $("#locations").val();
var inputvalue7 = $("#dropdown1").val();
var inputvalue8 = $("#dropdown2").val();

if(inputvalue!=null && inputvalue!="")
{
s = s+ "FilterField"+count+"=Journal&FilterValue"+count+"="+inputvalue+"&";
count++;
}
if(inputvalue2!=null && inputvalue2!="")
{
s = s+ "FilterField"+count+"=KeyWords&FilterValue"+count+"="+inputvalue2+"&";
count++;
}
if(inputvalue3!=null && inputvalue3!="")
{
s = s+ "FilterField"+count+"=datepub&FilterValue"+count+"="+inputvalue3+"&";
count++;
}
if(inputvalue4!=null && inputvalue4!="")
{
s = s+ "FilterField"+count+"=Title&FilterValue"+count+"="+inputvalue4+"&";
count++;
}
if(inputvalue5!=null && inputvalue5!="")
{
s = s+ "FilterField"+count+"=localcurrency&FilterValue"+count+"="+inputvalue5+"&";
count++;
}
if(inputvalue6!=null && inputvalue6!="")
{
s = s+ "FilterField"+count+"=locations&FilterValue"+count+"="+inputvalue6+"&";
count++;
}
if(inputvalue7!=null && inputvalue7!="")
{
s = s+ "FilterField"+count+"=dropdown1&FilterValue"+count+"="+inputvalue7+"&";
count++;
}
if(inputvalue8!=null && inputvalue8!="")
{
s = s+ "FilterField"+count+"=dropdown2&FilterValue"+count+"="+inputvalue8+"&";
count++;
}
window.location.replace("/teamsites/Bib%20Test/Forms/search.aspx?"+s);

});
});
</script>

Journal <input type="text" id="journal">
keywords <input type="text" id="keywords">




<select id="datepub">
<option value=""></option>
<option value="1950">1950</option>
<option value="2010">2010</option>
<option value="2017">2017</option>
<option value="audi">Audi</option>
</select>

title
<select id="title">
<option value=""></option>
<option value="TestDoc">test doc</option>
<option value="t">t</option>

</select>

localcurrency
<select id="localcurrency">
<option value=""></option>
<option value="USD">USD</option>

</select>

locations
<select id="locations">
<option value=""></option>
<option value="US">US</option>
<option value="UK">UK</option>

</select>

dropdown1
<select id="dropdown1">
<option value=""></option>
<option value="a">a</option>
<option value="b">b</option>

</select>

dropdown2
<select id="dropdown2">
<option value=""></option>
<option value="aa">aa</option>
<option value="bb">bb</option>
<option value="cc">cc</option>
<option value="dd">dd</option>
</select>
<button type="button" id="button">search</button>

当用户点击搜索按钮时,用户将被重定向到 /teamsites/Bib%20Test/Forms/search.aspx 页面,其中包含 url 中的过滤器参数,这将根据传递的参数显示相关记录。

现在过滤工作正常..但我面临的问题是,在我将用户重定向到此页面 /teamsites/Bib%20Test/Forms/search.aspx 后,过滤字段值(如本地货币、地点、标题等。)将被清除。那么我可以使用 JavaScript 为文件管理器字段分配其原始值吗?我的意思是我可以从 URL 中提取字段的值并将其分配给它们吗?所以在用户被重定向到 /teamsites/Bib%20Test/Forms/search.aspx 之后,他们仍然可以看到用他们输入的过滤值填充的过滤字段?

最佳答案

您可以通过执行以下操作来完成此操作:

  • 解析当前页面的查询字符串,分离出其查询参数
  • 处理这些以匹配过滤器字段名称及其值
  • 使用$('#' + fieldName).val(value)设置页面中的字段值

下面,作为演示,我将硬编码查询字符串 '?FilterField0=locations&FilterValue0=US&FilterField1=dropdown1&FilterValue1=b' 传递到 populateSearchFields() 函数中.在实践中,您将使用此处的三个函数,不加修改,但不是硬编码值,而是传入 window.location.search

// input: '?a=b&b=c&e=f'
// output: { a: 'b', b: 'c', e: 'f' }
function buildParameterMap(queryString) {
// ignore the ? at the beginning and split the query string into
// pieces separated by &
var pairs = queryString.replace(/^\?/, '').split('&');
var map = {};

pairs.forEach(function(pair) {
// further split each piece to the left and right of the =
// ignore pairs that are empty strings
if (pair) {
var sides = pair.split('=');
map[sides[0]] = decodeURIComponent(sides[1]);
}
});

return map;
}

// input: { FilterField0: 'Name', FilterValue0: 'Fred',
// FilterField1: 'age', FilterValue1: '30' }
// output: { name: 'Fred', age: '30' }
function buildFilterFieldMap(parameterMap) {
var maxFieldCount = 15;

var map = {};

for (var i = 0; i < maxFieldCount; i += 1) {
var filterFieldName = parameterMap['FilterField' + i];
if (filterFieldName) {
map[filterFieldName.toLowerCase()] = parameterMap['FilterValue' + i];
}
}

return map;
}

function populateSearchFields(queryString) {
// build a map from URL query string parameter -> value
var parameterMap = buildParameterMap(queryString);

// build a map from search field name -> value
var filterFieldMap = buildFilterFieldMap(parameterMap);

Object.keys(filterFieldMap).forEach(function(field) {
$('#' + field).val(filterFieldMap[field]);
});
}

populateSearchFields('?FilterField0=locations&FilterValue0=US&FilterField1=dropdown1&FilterValue1=b');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Journal <input type="text" id="journal"> keywords <input type="text" id="keywords">


<select id="datepub">
<option value=""></option>
<option value="1950">1950</option>
<option value="2010">2010</option>
<option value="2017">2017</option>
<option value="audi">Audi</option>
</select> title
<select id="title">
<option value=""></option>
<option value="TestDoc">test doc</option>
<option value="t">t</option>

</select> localcurrency
<select id="localcurrency">
<option value=""></option>
<option value="USD">USD</option>

</select> locations
<select id="locations">
<option value=""></option>
<option value="US">US</option>
<option value="UK">UK</option>

</select> dropdown1
<select id="dropdown1">
<option value=""></option>
<option value="a">a</option>
<option value="b">b</option>

</select> dropdown2
<select id="dropdown2">
<option value=""></option>
<option value="aa">aa</option>
<option value="bb">bb</option>
<option value="cc">cc</option>
<option value="dd">dd</option>
</select>
<button type="button" id="button">search</button>

关于javascript - 我如何根据 URL 中的参数填充我的搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com