添加广告点击发送触发Uncaught TypeError,特定分类异常求助
Hey there! Let's break down why this error only pops up in the "工作 » IT/电信/计算机" category and how to fix it.
错误根源
The Uncaught TypeError: Cannot read property 'top' of undefined error stems directly from this line in your code:
var firstErrorMessageTop = form.find('.error-tooltip-container, .main-form-error').first().offset().top;
When you submit the form in that specific category, jQuery can't find any elements matching .error-tooltip-container or .main-form-error. So .first() returns an empty jQuery object, calling .offset() on it gives undefined, and trying to access .top on undefined triggers the error.
为什么只有这个分类出问题?
There are a few likely scenarios:
- Missing error DOM elements: The form in this category might not include the
.error-tooltip-containeror.main-form-errorelements in its HTML structure, unlike other working categories. - Validation/response mismatch: Maybe the backend returns an error status for this category, but the frontend fails to render the corresponding error tooltip elements. Or conversely, the form passes validation but the code accidentally enters the error-handling branch.
- Async loading issue: The error elements might load asynchronously for this category, and your code runs before they're added to the DOM.
快速修复(先解决报错)
Add a check to ensure we only access .offset().top if the error element actually exists:
form.find('.info-tooltip-container').addClass('hidden'); // First get the error element and verify it exists var firstErrorElement = form.find('.error-tooltip-container, .main-form-error').first(); if (!form.hasClass('ajax-form-no-scrolling') && firstErrorElement.length > 0) { var firstErrorMessageTop = firstErrorElement.offset().top; $(window).scrollTop(firstErrorMessageTop - 10); }
This will stop the TypeError from firing when no error elements are found.
后续排查(解决根本问题)
To fix the underlying issue, you’ll need to:
- Inspect the DOM: Check if
.error-tooltip-containerand.main-form-errorexist in the HTML of the problematic category's form. If not, add them to match the structure of other working categories. - Check backend responses: Use browser dev tools to see if the response status is
errororsuccesswhen submitting the form. If it’serror, ensure the frontend correctly renders error elements for this category. - Compare validation logic: Verify if the form validation rules for this category differ from others—maybe missing required fields cause an unexpected response that doesn’t trigger the error UI.
内容的提问来源于stack exchange,提问作者Memo

