我想制作4个单选按钮和1个转到按钮,我想当我检查任何一个单选按钮,然后点击转到按钮时,url将被更改



世界!谷歌
脸书
雅虎
推特
点击转到✓if(document.getElementById('gogle').checked=true);https://google.com");}else{document.getElementById('btn').setAttribute("href&",";https://example.com");}if(document.getElementById('facebook').checked=true){document.getElement ById('dtn').setAttribute("href"https://facebook.com");}else{document.getElementById('btn').setAttribute("href&",";https://example.com");}if(document.getElementById('yahoo').checked=true){document.getElement ById('dtn').setAttribute("href&";https://yahoo.com");}else{document.getElementById('btn').setAttribute("href&",";https://example.com");}if(document.getElementById('witter').checked=true){document.getElement ById('dtn').setAttribute("href"https://twitter.com");}else{document.getElementById('btn').setAttribute("href&",";https://example.com");}

给你。。。。。。您只需要使用javascript的window.location dom操作函数来重定向用户。您可以使用if-else语句来检查使用querySelector或任何其他方式选择了哪个站点。这是我的代码。

<form>
<input type="radio" name="site" id="google" /> Google <br />
<input type="radio" name="site" id="facebook" /> Facebook <br />
<input type="radio" name="site" id="yahoo" /> Yahoo <br />
<input type="radio" name="site" id="twitter" /> Twitter <br />
<hr />
<button type="submit" onclick="redirect()">Click to go</button>
</form>

下面的javascript代码:-

function redirect() {
if (document.getElementById('google').checked) {
document.write("Redirecting...");
window.location = ("https://www.google.com");
}
else if (document.getElementById('facebook').checked) {
document.write("Redirecting...");
window.location = ("https://www.facebook.com");
}
else if (document.getElementById('yahoo').checked) {
document.write("Redirecting...");
window.location = ("https://www.yahoo.com");
}
else if (document.getElementById('twitter').checked) {
document.write("Redirecting...");
window.location = ("https://www.twitter.com");
}
else {
return;
}
}

最新更新