3复选框以不同的更新参数
我有一系列if语句来确定location.href
参数。我以前从未设置过这样的东西。
我想要tarput =
,usernum =
和coneff =
以更新URL中的参数。但是由于某种原因,我的用户室破坏了JS。
这是我的示例:
function goPBFour(event) {
//something wrong with number of users updating the url
event.preventDefault();
const formData = new FormData(event.target);
const userNum = formData.get("userNum");
if (document.querySelector("input[name=throughput]").checked) {
if (document.querySelector("input[name=user]").checked) {
if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&userNum=" + userNum + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&userNum=" + userNum;
}
} else if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&conEff=true";
}
} else if (document.querySelector("input[name=user]").checked) {
if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&userNum=" + userNum + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search + "&userNum=" + userNum;
}
} else if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search;
}
}
<form id="myForm" onsubmit="goPBFour(event)" method="get">
<div id="pBFContainer" class="container">
<div id="bodyFOption1">
<input type="checkbox" name="throughput" value="yes" />Would you like to test the user experience throughput?
<p></p>
</div>
<div id="bodyFOption2">
<input type="checkbox" name="user" value="yes" onsubmit="goPBFour(event)" />Would you like to test capacity performance (concurrent users)?<br /> How many Users:<input type="number" id="numberUsers" size="3" name="userNum" placeholder="2" min="2"
max="12" disabled required/> (Evaluate limits 2-12 users)
<p></p>
</div>
<div id="bodyFOption3"><input type="checkbox" name="conEff" value="yes" />Would you like to test connection efficiency?</div>
</div>
<input type="submit" id="subButton" value="Next..." />
</form>
<script type="text/javascript" src="evalportalp1.js"></script>
I have a series of if statements to determine location.href
parameters. I have never set up something like this before.
I want throughput=
, userNum=
, and conEff=
to update as parameters in the URL. but for some reason, my userNum breaks the JS.
Here is my example:
function goPBFour(event) {
//something wrong with number of users updating the url
event.preventDefault();
const formData = new FormData(event.target);
const userNum = formData.get("userNum");
if (document.querySelector("input[name=throughput]").checked) {
if (document.querySelector("input[name=user]").checked) {
if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&userNum=" + userNum + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&userNum=" + userNum;
}
} else if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&throughput=true" + "&conEff=true";
}
} else if (document.querySelector("input[name=user]").checked) {
if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&userNum=" + userNum + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search + "&userNum=" + userNum;
}
} else if (document.querySelector("input[name=conEff]").checked) {
window.location.href = "evalportalb4.html" + location.search + "&conEff=true";
} else {
window.location.href = "evalportalb4.html" + location.search;
}
}
<form id="myForm" onsubmit="goPBFour(event)" method="get">
<div id="pBFContainer" class="container">
<div id="bodyFOption1">
<input type="checkbox" name="throughput" value="yes" />Would you like to test the user experience throughput?
<p></p>
</div>
<div id="bodyFOption2">
<input type="checkbox" name="user" value="yes" onsubmit="goPBFour(event)" />Would you like to test capacity performance (concurrent users)?<br /> How many Users:<input type="number" id="numberUsers" size="3" name="userNum" placeholder="2" min="2"
max="12" disabled required/> (Evaluate limits 2-12 users)
<p></p>
</div>
<div id="bodyFOption3"><input type="checkbox" name="conEff" value="yes" />Would you like to test connection efficiency?</div>
</div>
<input type="submit" id="subButton" value="Next..." />
</form>
<script type="text/javascript" src="evalportalp1.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的JS有一些问题,我使用较少的重复使它变得更简单了,
缺少
&lt;/div;/div;
,然后移动了&lt; script&gt;&gt;&gt;/script&gt; <<<<<<<<<<
/代码>到&lt; body&gt;
的内部。在阅读有关
location.href
的时候,我来了 https://developer.mozilla.org/en-us/docs/web/api/location ,也许最好使用location.replace.replace()
?There were a few issues with your js, I made it somewhat simpler using less repetition
There was a missing
</div>
and I moved the<script></script>
to the inside of<body>
.While reading about
location.href
, I came about this https://developer.mozilla.org/en-US/docs/Web/API/Location, maybe it would be better to uselocation.replace()
?您可以通过将
formdata
与urlsearchParams
对象相结合,使其变得更加容易和动态。在您的示例中因此,您已经可以使用它,而不是检查每个单独的输入。
通过将
location.search
传递到新的urlsearchParams
实例来获取当前的URL参数。然后在formdata
对象上循环,然后将每个密钥和值传递给urlsearchParams
对象。结果应该是从表单中值覆盖的URL中的当前参数。You can make this significantly easier and dynamic by combining the
FormData
with anURLSearchParams
object.In your example
FormData
extracts all the values from the form and does that in a smart way by omitting inputs that are eitherdisabled
or notchecked
, so you can already use that instead of checking every individual input.Get the current URL params by passing
location.search
to a newURLSearchParams
instance. Then loop over theFormData
object and pass every key and value to theURLSearchParams
object. The result should be the current params in the URL overwritten by the values from the form.有很多问题,我试图重写它。这是我
的逻辑过度复杂的地方,而不是干燥(不要重复自己)
,我对此越多,我想知道为什么不拥有表格呢?
如果您在选择上删除残疾人,则表格应该有效
There are many issues, I tried to rewrite it. This is where I am
Your logic is overcomplicated, AND not DRY (Don't Repeat Yourself)
The more I looked at this, I wondered why not just have the form?
Just the form should work if you remove the disabled on the select