如何使用单选按钮将用户发送到JavaScript中的新网站? [英] How do I use a radio button to send a user to a new website in JavaScript?
问题描述
我想要的程序是做一个表单,并有2个单选按钮和1个文本。
然后我希望它将文本和电台值合并为一个并带我到那个页面:
如果我用类似facebook的方式输入文本并这个单选按钮的值是.com,我希望它带上facebook + .com并将我发送到该页面。
<!doctype html><< ;>< head>< title>< head>< title>< body onunload =Bye() ><形式> <字段集> <图例>重定向:< / legend> < DIV> < label>您要去哪里?< / label> < input type =textid =inputname =inputsize =7> < input type =submitid =submitname =submitvalue =Submitonclick =go()> < / DIV> < DIV> < input type =radioid =noname =endvalue =。no> < label for =。no> .no< / label> < br /> < input type =radioid =comname =endvalue =。com> < label for =。com> .com< / label> < / DIV> < / fieldset>< / form>< script type =text / javascript>函数go(){var end =; if(document.getElementById(no)。checked){end = document.getElementById(no).value; } else {end = document.getElementById(com).value; } var input = document.getElementById(input).value; var together = input + end; window.location.replace(http:// www。+ together); $ lt; / body>< / html>
h2_lin>解决方案
或者,从onclick处理程序返回false以防止表单提交。 试试这段代码:
< html>
< head>
< / head>
< body>
< form>
< fieldset>
< legend>重定向:< / legend>
< div>
< label>您要去哪里?< / label>
< input type =textid =inputname =inputsize =7>
< input type =submitid =submitname =submitvalue =Submitonclick =return go()>
< / div>
< div>
< input type =radioid =noname =endvalue =。no>
< label for =。no> .no< / label>
< br />
< input type =radioid =comname =endvalue =。com>
< label for =。com> .com< / label>
< / div>
< / fieldset>
< / form>
< script type =text / javascript>
函数go(){
var end =;
if(document.getElementById(no)。checked){
end = document.getElementById(no)。value;
} else {
end = document.getElementById(com).value;
}
var input = document.getElementById(input)。value;
var together = input + end;
window.location.replace(http:// www。+ together);
返回false;
}
< / script>
< / body>
< / html>
brso05 的分析似乎是现货......但我无法真正解释它。看起来,Chrome延迟了 location.href.replace
(应从页面导航)的副作用,直到表单提交后...我有一个感觉你在这里遇到了浏览器错误。我无法想象这是符合规范的。
What I want the program to do is make a form and have 2 radio buttons and 1 text. Then I want it to collapse the text and radio value together into one and take me to that page:
If I input text with like "facebook" and the radiobutton value is .com I want it to take facebook + .com and send me to that page.
<!doctype html>
<html>
<head>
<title>A Basic Form</title>
<style type="text/css">
</style>
</head>
<body onunload="Bye()">
<form>
<fieldset>
<legend>Redirection: </legend>
<div>
<label>Where do you want to go?</label>
<input type="text" id="input" name="input" size="7">
<input type="submit" id="submit" name="submit" value="Submit" onclick="go()">
</div>
<div>
<input type="radio" id="no" name="end" value=".no">
<label for=".no">.no</label>
<br />
<input type="radio" id="com" name="end" value=".com">
<label for=".com">.com</label>
</div>
</fieldset>
</form>
<script type="text/javascript">
function go() {
var end = "";
if (document.getElementById("no").checked) {
end = document.getElementById("no").value;
} else {
end = document.getElementById("com").value;
}
var input = document.getElementById("input").value;
var together = input + end;
window.location.replace("http://www." + together);
}
</script>
</body>
</html>
Alternatively, return false from the onclick handler to prevent the form submit.
Try this code:
<html>
<head>
</head>
<body>
<form>
<fieldset>
<legend>Redirection: </legend>
<div>
<label>Where do you want to go?</label>
<input type="text" id="input" name="input" size="7">
<input type="submit" id="submit" name="submit" value="Submit" onclick="return go()">
</div>
<div>
<input type="radio" id="no" name="end" value=".no">
<label for=".no">.no</label>
<br />
<input type="radio" id="com" name="end" value=".com">
<label for=".com">.com</label>
</div>
</fieldset>
</form>
<script type="text/javascript">
function go() {
var end = "";
if (document.getElementById("no").checked) {
end = document.getElementById("no").value;
} else {
end = document.getElementById("com").value;
}
var input = document.getElementById("input").value;
var together = input + end;
window.location.replace("http://www." + together);
return false;
}
</script>
</body>
</html>
brso05's analysis seems to be spot on... But I can't really explain it. It seems that Chrome is delaying the side effects of the location.href.replace
(which should be navigating away from the page) until after the form submit... I have a feeling you have hit a browser bug here. I can't imagine this is spec-compliant.
这篇关于如何使用单选按钮将用户发送到JavaScript中的新网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!