正在更改Url参数
Changing Url parameters
我正试图根据我点击的按钮来更改URL参数。在这里,我设置了一些javascript来根据点击的按钮更改屏幕显示的内容:
<script language="Javascript">
//How many Questions
function Questions(){
document.getElementById("Questions").style.visibility="visible";
document.getElementById("Manual").style.visibility="hidden";
document.getElementById("Load").style.visibility="hidden";
}
//Manual
function Manual(){
document.getElementById("Questions").style.visibility="hidden";
document.getElementById("Manual").style.visibility="visible";
document.getElementById("Load").style.visibility="hidden";
}
//Load Data from Form
function Load(){
document.getElementById("Questions").style.visibility="hidden";
document.getElementById("Manual").style.visibility="hidden";
document.getElementById("Load").style.visibility="visible";
}
</script>
<!--VT Buttons-->
<div id="enterDataToggle" class="btn-group" data-toggle="buttons" >
<!--VT Defualt-->
<label class="btn btn-default active" id="checkin-label"
<input type="radio" checked onClick="Questions()">
Default
</label>
<!--VT Input Data Manually-->
<label class="btn btn-default active" id="checkin-label"
<input type="radio" onClick="Manual()">
Input Data Manually
</label>
<!--VT Load Data from File-->
<label class="btn btn-default active" id="checkin-label"
<input type="radio" onClick="Load()">
Load Data from File
</label>
这里是我设置URL参数的地方:
<!--VT How many Questions-->
<div style="position:absolute" id="Questions" >
<h4>How many questions were on the ballot?</h4>
<input name="votingDist" type="radio" value="-1"=> Short: 1-3 questions<br>
<input name="votingDist" type="radio" value="0"> Medium: 4+ qustions<br>
<input name="VTMean" value="61.11"; style= "visibility:hidden;" type="text" style="width:100%">
<input name="VTMean" value="128.69"; style= "visibility:hidden;" type="text" style="width:100%">
<input name="VTStd" value="-1"; style= "visibility:hidden;" type="text" style="width:100%">
</div>
<!-- VT Input Data Manually-->
<!--CT Default-->
<div style=" visibility:hidden; position:absolute" id="Manual" >
<input name="votingDist" value="1"; style= "visibility:hidden;" type="text" style="width:100%">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>
Mean
</th>
<th>
Standard Deviation
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input name="VTMean" type="text" style="width:100%">
</th>
<th>
<input name="VTStd" type="text" style="width:100%">
</th>
</tr>
</tbody>
</table>
</div>
<!-- Load Data from File-->
<div style="visibility:hidden; position:relative" id="Load">
<h4>Load Data from File</h4>
<input name="arrivalFile" type="file" name="fileField"><br>
</div>
问题是我的URL出现了重复的参数:http://localhost:8080/CrunchifyJSPServletExample/CrunchifyServlet?votingDist=-1&VTMean=61.11&VTMean=128.69&VTStd=-1&votingDist=1&VTMean=200&VTStd=20
我正试图找到一种方法,当单击不同的按钮时替换参数的值。有办法做到这一点吗?非常感谢您的帮助!:)
您可以使用对象并设置属性。因此,如果您再次设置,它将覆盖该值。然后您可以将其转换为url参数格式:
var params = {};
params['votingDist'] = -1;
params['votingDist'] = 2;
params['VTMean'] = 200;
params['VTMean'] = 300;
var urlParamsToSet = Object.keys(params)
.reduce((a, b) => `${a}${a ? '&' : '?'}${b}=${params[b]}`, '');
// ?votingDist=2&VTMean=300
然后使用urlParamsToSet
更改url。
相关文章:
- 通过Javascript将HTML中的电话号码与URL参数进行交换
- 如何使用Javascript从URL参数中自动填充文本字段
- 如何将Google Analytics配置为将URL参数作为单个页面进行跟踪
- 在url参数javascript中存储键值对列表
- backbone.js-映射标准url参数-使用多个参数
- 将URL参数传递给ui sref
- Javascript从字符串中剥离所有url参数
- Javascript/jQuery获取所有url参数并添加/更改一个参数
- 在页面加载完成之前,使用URL参数运行JavaScript
- 使用Javascript获取URL参数
- 如何在 JavaScript 中解析 URL 参数
- 主干.js将 url 参数传递给集合
- 谷歌应用脚本中的 UrlFetchApp.fetch 删除 url 参数
- 通过javascript将url参数传递到多个不同的href链接中
- 角度 1 - 获取当前 URL 参数
- 使用jquery获取url参数
- 在Javascript代码中插入URL参数
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- 限制与AngularJS状态匹配的动态URL参数的最佳方式
- 如何根据URL参数生成包含JS内容的单个HTML文件