我想制作4个广播按钮和1个go按钮,当我检查任何一个广播按钮然后单击GO按钮时,我想要的,然后将更改URL

发布于 2025-01-29 07:12:35 字数 936 浏览 2 评论 0原文

World! google
facebook
yahoo
twitter
click to Go✓ if(document.getElementById('google').checked = true) { document.getElementById('btn') .setAttribute("href", "https://google.com"); } else { document.getElementById('btn') .setAttribute("href", "https://example.com"); } if(document.getElementById('facebook').checked = true) { document.getElementById('btn') .setAttribute("href", "https://facebook.com"); } else { document.getElementById('btn') .setAttribute("href", "https://example.com"); } if(document.getElementById('yahoo').checked = true) { document.getElementById('btn') .setAttribute("href", "https://yahoo.com"); } else { document.getElementById('btn') .setAttribute("href", "https://example.com"); } if(document.getElementById('twitter').checked = true) { document.getElementById('btn') .setAttribute("href", "https://twitter.com"); } else { document.getElementById('btn') .setAttribute("href", "https://example.com"); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2025-02-05 07:12:35

在这里,您只需使用窗口即可使用javaScript的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;
        }
    }

Here you go...... You just have to use window.location dom manipulation function of javascript to redirect the user. You can use if-else statement to check which site was selected using querySelector or by any other means. Here is my code for it.

<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 code below :-

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文