如何使用 jQuery 自动完成组合框设置选定的值
我们在项目中使用自动完成组合框,并定义一个选择列表,其中包含从调用 api。我添加了第一个选项“全部”,我也添加了选定的属性,但组合框没有选择它,并且在加载后在框中显示“全部”。在绑定后,我也尝试将组合框的 val 设置为“全部”值,但我在那里没有任何乐趣。如何选择默认值?
We're using the autocomplete combobox for a project and define a select list with options added from a call to an api. I'm adding a first option of 'all' which i'm adding the selected attribute too but the combobox is not picking this up and having 'All' show in the box once it's loaded. I've tried setting the val of the combobox to the 'All' value also, after it's been bound but I've had no joy there. How can I select a default value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该能够以通常的方式将其中一个
元素设置为选定的:
您需要在将组合框绑定到
它查找选定的子项,并将文本输入的值设置为它找到的值,并使用空字符串作为后备。不过,这里没有使用
I think you should be able to just set one of the
<option>
elements as selected in the usual way:You need to do this before binding the combobox to the
<select>
. If you look at the source for the combobox, you'll see this:It looks for the selected children and sets the text input's value to what it finds with an empty string as a fallback. The default "select the first item" that a
<select>
element uses is not used here though, you have to explicitly mark an<option>
as selected or the:selected
selector will not, um, select it.