如何使用 jquery 自动完成组合框设置默认值?
使用jquery ui autocomplete combobox时,可以设置默认值吗组合框的值?
When using the jquery ui autocomplete combobox, can you set a default value for the combobox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我尝试以我自己的项目中的方式回答这个问题。
我从您发布的页面上阅读了演示源代码。在生成自动完成组合框的 jquery 代码中,我添加了一行代码,该代码在创建组合框时进行处理,从“select”元素中读取所选值。通过这种方式,您可以通过编程方式设置默认值(就像您通常不使用自动完成组合框一样)
这是我添加的一行:
简单明了。它将输入值设置为 #combobox 中所选元素的文本值。当然,您会想要更新 id 元素以匹配您的个人项目或页面。
这是在上下文中:
I tried answer this in the way that I would do it in my own project.
I read through the demo source code from the page you posted. In the jquery code that generates the autocomplete combobox, I added one line of code that processes when the combobox is created that reads the selected value from your "select" element. This way you can programmatically set the default value (like you would normally if you were not using the autocomplete combobox)
Here is the one line I added:
Plain and simple. It sets the value of input to the text value of the selected element from #combobox. Naturally, you will want to update the id elements to match your individual project or page.
Here it is in context:
基于Mathieu Steele 答案,而不是使用这个:
我使用这个:
Widget 现在是可重用的并且是 DRY :)
Based on Mathieu Steele answer, instead of using this:
I use this:
Widget is now reusable and DRY :)
答案#1 非常接近,但如果您想保持函数通用,则不能对元素 ID 进行硬编码。添加此行即可享受!
Answer #1 is very close, but you cannot hard-code the element ID if you want to keep the function generic. Add this line instead and enjoy!
您可以通过编辑自动完成器的以下语句来实现此目的:
You can achieve this by editing the following statement of the auto-completer:
我已经调整了此处的响应,以使用组合框已定义的选择变量来查找所选选项并使用它。这意味着它对于您在其上定义组合框的任何元素(使用 id、class 或选择器)都是通用的,并且也适用于多个元素。
希望这对某人有帮助!
I've tweaked the responses here to use the select variable already defined by the combobox to find the selected option and use that. This means it is generic for which ever element you have defined the combobox on (using id, class or selector ) and will work for multiple elements also.
Hope this helps someone!
将方法添加到 jquery 组合框脚本
add method to jquery combobox script
初始化后,调用
option
方法来设置框的值。Call the
option
method to set the box's value after you've initialized it.可能这会对您有所帮助
http://forum.jquery.com/topic/autocomplete-默认值
may be this will help you
http://forum.jquery.com/topic/autocomplete-default-value
我使用下面的代码行,只是实现相同结果的另一种方法:)
I used the below line of code instead, just another way of achieving the same result :)
我有一个适用于我的 jquery UI 组合框实现的答案。代码中间的某个地方是这样的:
我将其更改为:
很快,底层文本框的初始值出现了。在我看来这应该是默认功能,但我知道什么?
I have an answer that worked for my implementation of the jquery UI combobox. Somewhere in the middle of the code was this:
I changed it to:
and presto, the initial value of the underlying textbox appeared. Seems to me like this should be the default functionality, but what do I know?
在“
this._on( this.input, {
” 行之前添加以下一行代码在自动完成组合框脚本中创建代码后, 。
您还可以使用 html 的重置按钮进行重置。
Add below One Line of Code before "
this._on( this.input, {
" lineafter create code in autocomplete combobox script.
You can also reset with reset button of html.
我遇到了这个错误,无论我编码什么,都无法修复它的 OnLoad 事件(可能需要 3 小时)。最后幸运的是我遇到了 http://www .onemoretake.com/2011/04/17/a-better-jquery-ui-combo-box/ 网页(感谢@Dan解决了我的头痛问题)并看到真正缺失的部分不在“OnLoad”上”,关于 ui 定义函数本身。官方 Jquery Ui 网页上的标准定义函数不包含以编程方式选择选项。
这是应该添加到定义函数中的函数:
我还生成了另一个函数来通过选项文本而不是选项值更改选择
您可以在 OnLoad 或另一个函数上使用这些函数,如下所示:
或
I stumped this error and cannot fix it OnLoad event whatever I coded (Probably on 3 hours). At last luckily I acrossed http://www.onemoretake.com/2011/04/17/a-better-jquery-ui-combo-box/ web page(Thx for @Dan for solving my headache) and saw the real missing part is not on "OnLoad", on ui definition function itself. Standart definition function on the official Jquery Ui web page does not contain programmatically select option.
Here is the function should added to definition function :
also i produced another function to change selection via option text not option value
You can use these functions on OnLoad or another function as :
or
我通过在初始页面或运行时调用此函数解决了这个问题。例子:
I solved it by calling this function on the initial page or runtime. Example: