如何让 jQuery 自动完成在用户点击提交时执行 search()
作为我上一篇文章的后续内容 - 如何修剪 JQuery 自动完成框的输入? 。我决定提出一个新问题,而不是继续编辑那个问题。
我目前的 jQuery ui 自动完成 1.8(左右)工作得相当好,除了一件事。如果用户只是输入(有效)名称并点击提交,则自动完成查找永远不会运行,因此关联的值永远不会被捕获并分配给输入框。这当然是一个问题。阅读文档,这意味着我可以使用 jQuery search()
方法来克服这个问题。然而,我正在努力让它发挥作用。我尝试过在文本输入的 onblur 事件和提交按钮的 onclick 事件中调用 search()
,但没有效果 - 每当我点击提交时,我就没有任何值对于输入框。代码:
<form action = "<?php echo $this->URL();?>" method="post">
<fieldset>
<ol>
<li>
<label for="Resource">Resource</label>
<input id="Resource" name="Resource" class="text" type="text" value="" onblur="jQuery('#Resource').search();"/>
</li>
</ol>
</fieldset>
<fieldset class="submit">
<input id="Submit" name="Submit" type="Submit" class="Submit" value ="Submit" onclick = "jQuery('#Resource').search();"/>
</fieldset>
</form>
那么,我到底做错了什么?
As a follow on my from last post - How would I trim the input to a JQuery auto-complete box? . I decided to make a new question rather than to continue editing that one.
I currently have jQuery ui autocomplete 1.8 (or so) working reasonably well, except from one thing. If the user just enters a (valid) name and hits submit, the auto-complete lookup never runs, and so the associated value is never grapped and assigned to the input box. This, of course, is a problem. Reading the documentation, it implies that I could use the jQuery search()
method to over-come this. However, I am struggling to get it to work. I have tried both putting the call to search()
in the onblur event of the text input, and in the onclick event of the submit button, but to no avail - whenever I hit submit I have no value for the input box. The code:
<form action = "<?php echo $this->URL();?>" method="post">
<fieldset>
<ol>
<li>
<label for="Resource">Resource</label>
<input id="Resource" name="Resource" class="text" type="text" value="" onblur="jQuery('#Resource').search();"/>
</li>
</ol>
</fieldset>
<fieldset class="submit">
<input id="Submit" name="Submit" type="Submit" class="Submit" value ="Submit" onclick = "jQuery('#Resource').search();"/>
</fieldset>
</form>
So, what exactly am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在change事件中添加automatic-choose-any-matching-result代码。
检查我对你的另一个问题的回答:
jQuery 自动完成问题 - 如果用户没有专门选择,则不匹配
You need to add the automated-choose-any-matching-result code in the change event.
Check my answer on your other question:
jQuery autocomplete problem - doesn't match if user doesn't specifically select
我都回答了我自己的问题,并且还发现
search()
并不像我想象的那样工作。我尝试过:发现在表单提交完成前一秒,自动完成框会闪烁。显然
search()
只是让自动完成框进行正常搜索,而不是自动选择任何匹配结果搜索,这正是我正在寻找的。I have both answered my own question, and also discovered that
search()
doesn't work like I thought. I tried:And discovered that a second before the form finishes submitting, the auto-complete box flashes up. Obviously
search()
just makes the auto-complete box do a normal search, not an automated-choose-any-matching-result search, which is what I was looking for.