complete.ly库选项不显示问题求助:基础示例故障排查
Hey there! Let's dig into why your complete.ly text box options aren't displaying as expected. Looking at your code snippet, there are a couple of straightforward fixes that should get things working:
Common Issues & Fixes
1. Missing Initial Options Setup
Your code only sets pv.options when specific text is entered, but you never initialize it with your strategyChoice array. That means when the user starts typing, there are no options to display initially.
2. Whitespace Mismatch in Text Matching
Your condition checks for text === 'I want to ' (with a trailing space), but users might not type that space—so the condition never triggers, and the options never update.
3. Incomplete State Management
There's no fallback to reset options when the user moves away from your special cases, which could leave the dropdown stuck with no options later.
Corrected Code Example
Here's a revised version of your code that addresses these issues:
var strategyChoice = ["a", "b", "c"]; $(function() { // Initialize complete.ly on your container var pv = completely(document.getElementById('container')); // Set initial options so dropdown works from the start pv.options = strategyChoice; pv.onChange = function(text) { console.log(text); const trimmedText = text.trim(); // Normalize text to avoid whitespace issues if (trimmedText === 'i') { pv.setText('I want to'); // Update options immediately after setting the new text pv.options = ["pay", "receive"]; pv.repaint(); return; } if (trimmedText === 'I want to') { pv.options = ["pay", "receive"]; pv.repaint(); } else { // Fall back to initial options when not in special case pv.options = strategyChoice; pv.repaint(); } }; });
Additional Checks
Make sure your HTML container is a plain empty div—complete.ly generates its own input and dropdown elements inside it:
<div id="container"></div>
These changes should ensure your dropdown options show up both initially and when hitting your special text cases. Let me know if you run into any other snags!
内容的提问来源于stack exchange,提问作者Chapo

