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

按钮触发A/B测试随机跳转功能失效,寻求技术解决

Fixing Your A/B Test Redirect Issue

Hey there! Let's figure out why your "立即注册" button isn't redirecting users randomly to your two success pages, and get it up and running smoothly.

First, Check for Errors

Before tweaking code, open your browser's developer tools (hit F12 or Ctrl+Shift+I) and look at the Console tab. Any red error messages here will point you straight to the problem—like missing jQuery, syntax errors, or broken paths.

Common Issues & Fixes

1. jQuery Isn't Loaded

Your code uses $, which is jQuery's shorthand. If jQuery isn't included on your page, this code won't run at all.

  • Fix: Make sure you've added the jQuery library before your custom JavaScript. You can host it locally on your server or use a trusted CDN (just ensure it's loaded early in your page).

2. Mismatched Redirect URLs

Your current JS code points to http://host/versionA and versionB, but you need to send users to /success-v1 or /success-v2. Using the wrong paths will either send users to the wrong place or throw a 404 error.

  • Fix: Update the URLs in your JS to match your actual target paths:
    window.location.href = "/success-v1"; // Use this for version 1
    window.location.href = "/success-v2"; // Use this for version 2
    

3. Typos in Element IDs

Double-check that your button's id (testbutton) is exactly the same in both your HTML and JavaScript. Even a tiny typo (like testButton with a capital B) will break the click event binding.

4. Button Loads Dynamically

If your "立即注册" button is added to the page after the initial load (e.g., loaded via AJAX or a dynamic component), the $(document).ready() binding won't catch it.

  • Fix: Use event delegation to attach the click event to a parent element that exists when the page first loads (like document):
    $(document).ready(function(){
      $(document).on('click', '#testbutton', function(){
        const targetUrl = Math.random() > 0.5 ? "/success-v1" : "/success-v2";
        window.location.href = targetUrl;
      });
    });
    

Working Full Code

Here's a cleaned-up version of your code that addresses all the common issues:

HTML

<!-- Ensure jQuery is loaded above this section -->
<div class="button" id="testbutton">立即注册</div>

JavaScript

$(document).ready(function(){
  $('#testbutton').click(function(){
    // 50/50 random split between the two success pages
    const redirectTo = Math.random() > 0.5 ? "/success-v1" : "/success-v2";
    window.location.href = redirectTo;
  });
});

Give these fixes a try, and let me know if you run into any more issues!

内容的提问来源于stack exchange,提问作者Max Di Campo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:28:54