scriptaculous ajax 自动完成空响应
我正在使用 ajax-autocompleter,效果很好。 我的目标是在现有项目的编辑功能或未找到项目的创建功能之间进行重定向。
我向每个 li 插入一个特定的 id,因此我可以使用 afterUpdateElement 选项将其用于编辑功能。
但是,如果没有找到结果,则列表为空,并且我找不到任何方法来告诉 afterUpdateElement 脚本没有找到结果。 事实上,没有调用 afterUpdateElement,因为没有选择。 所以 afterUpdateElement 是没用的......
我正在考虑测试 ajax 请求发送的完整值。 但我没找到怎么抢…
I’m working with the ajax-autocompleter which works great.
My aim is to redirect between an editing function for an existing item, or a creating function for a not-found item.
I insert a specific id to each li, and I can therefore use it for the editing function with an afterUpdateElement option.
But if no results are found, the list is empty, and I can’t find any way to tell the afterUpdateElement script that no results were found.
Indeed, no afterUpdateElement is called, since there is no selection. So afterUpdateElement is useless…
I was thinking about testing the full value sent by the ajax request.
But I didn’t find how to grab it…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以,我终于找到了一种方法来检查结果是否被选择(在 Gwyohm 的巨大帮助下):
如果用户更改搜索字段值,对于未选择的全新值,不会使用 afterUpdateElement,并且我们的隐藏值仍设置为 TRUE。
该问题的解决方案是听按下按键的声音。 如果按下某个键,则隐藏字段将返回 FALSE。 这适用于所有键,甚至是实际用于进行选择的 Enter 键。 所以这是一个我们应该从按键监听器中删除的异常。
所以,最后让我们看看我们的额外功能:
<块引用>
$(文本字段的id).observe("keyup", function(e) {
<块引用>
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(键码!=13){
<块引用>
$(id-of-hidden-field).value="FALSE";
}
}.bindAsEventListener($(文本字段的id)));
然后,我们可以使用隐藏字段来了解是否应编辑或创建文本值...
请注意:使用此解决方案,如果用户键入与现有值匹配的值而不选择它,它将被视为新值,如果您将此值添加到数据库中,将导致重复条目。 (但这对我来说没问题,因为我正在处理名字,两个不同的人可能有相同的名字......)
So, I finally found a way to check if results were selected or not (with the huge help of Gwyohm) :
If the user changes the search field value, for a brand new value not selected, no afterUpdateElement is used, and our hidden value is still set to TRUE.
The solution to that problem is to listen to a key pressed. If a key is pressed, then the hidden field turns back to FALSE. This works for all keys, even the Enter key actually used to make a selection. So this is an exception we should remove from our keypress listener.
So, finally let's have a look to our extra function :
We can then use the hidden field to know if the text value should be edited or created...
Beware : with this solution, if the user types a value matching an existing value, without selecting it, it will be considered as a new value, and will lead to a duplicate entry if you add this value to your database. (but this was ok for me, as I was working with names, and two different people may have the same name...)
您可以尝试像这样覆盖 updateChoices 方法:
在controls.js 中覆盖不是一个好主意。 相反,您可以将其添加到新的 .js 并将其包含在库之后。
编辑:pff .. 对于不好的缩进感到抱歉,但它是从 TextMate 复制/粘贴的,并且具有制表符和空格。 我希望你明白这一点。 我唯一添加的就是
块。 另外,我没有测试代码,但我认为它应该可以工作。
You can try to overwrite the updateChoices method like so:
It's not a good ideea to overwrite in controls.js. Instead you can add this to a new .js and include it after the library.
EDIT: pff.. sorry about the bad indents, but it's copy/paste from TextMate and is has tabs combined with spaces. I hope you get the point though. The only thing I added was the
block. Also, I didn't test the code, but I think it should work.