You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP根据用户输入实现页面重定向的技术求助

实现输入框触发页面重定向的方案

没问题!我帮你写个简单的纯前端实现,HTML+JavaScript就能搞定,直接用就行👇

完整代码示例

<!DOCTYPE html>
<html>
<head>
    <title>输入跳转</title>
    <style>
        /* 简单美化一下输入框和按钮,你可以自己调整样式 */
        .input-container {
            margin: 50px auto;
            text-align: center;
        }
        #redirectInput {
            padding: 8px 12px;
            font-size: 16px;
            width: 200px;
        }
        #submitBtn {
            padding: 8px 16px;
            font-size: 16px;
            margin-left: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="input-container">
        <input type="text" id="redirectInput" placeholder="输入a或b跳转">
        <button id="submitBtn">跳转</button>
    </div>

    <script>
        // 获取DOM元素
        const input = document.getElementById('redirectInput');
        const submitBtn = document.getElementById('submitBtn');

        // 定义跳转地址,你可以改成实际的网站链接
        const siteAUrl = 'https://site-a.example.com';
        const siteBUrl = 'https://site-b.example.com';

        // 跳转逻辑函数
        function handleRedirect() {
            const inputValue = input.value.trim().toLowerCase(); // 转小写,避免大小写问题
            if (inputValue === 'a') {
                window.location.href = siteAUrl;
            } else if (inputValue === 'b') {
                window.location.href = siteBUrl;
            } else {
                alert('请输入a或b哦!'); // 输入错误时提示
            }
            // 清空输入框
            input.value = '';
        }

        // 按钮点击触发跳转
        submitBtn.addEventListener('click', handleRedirect);

        // 回车键触发跳转,提升用户体验
        input.addEventListener('keydown', function(e) {
            if (e.key === 'Enter') {
                handleRedirect();
            }
        });
    </script>
</body>
</html>

代码说明

  • 我加了简单的样式,让页面看起来不那么简陋,你可以根据自己的需求调整CSS;
  • 输入值会自动转成小写,所以用户输入A或者a都能跳转到site a,更人性化;
  • 除了点击按钮,按回车键也能触发跳转,符合用户习惯;
  • 如果输入了其他内容,会弹出提示框提醒用户。

要是你需要调整跳转地址,直接修改代码里的siteAUrl和siteBUrl这两个变量就行,非常方便~

内容的提问来源于stack exchange,提问作者spacew4lker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:35:48