简体   繁体   English

jQuery自动完成-强制选择/测试

[英]jQuery Auto-complete - Force Selection/Testing

I have a simpe jQuery autocomplete input box with the ID 'medID' 我有一个ID为'medID'的simpe jQuery自动完成输入框

$('#medID').val("Par").focus().keydown();

I use the code above to put a value in the box, trigger focus and then key down to search. 我使用上面的代码在框中输入一个值,触发焦点,然后按下键进行搜索。

It returns between 1-5 values. 它返回1-5个值之间。

How do i get it automaticly select one of the suggested values? 我如何自动选择建议值之一?

I have tried: 我努力了:

$('#medID').children()[0].focus().click();

and

$('#medID').children().focus().click();

But the suggested auto complete values are direct chilren of the input box? 但是建议的自动完成值是输入框的直接值吗?

This is so i can test this part of my system without the need of a user. 这样一来,我可以在不需要用户的情况下测试系统的这一部分。

I'm not 100% certain, but I think browsers prohibit this. 我不确定100%,但是我认为浏览器禁止这样做。 Your request might turn out to be impossible. 您的请求可能无法实现。

If it were possible, a malicious script could gain access to data that users had previously typed into input fields, eg credit card numbers or email addresses, using a script just like the one you outlined. 如果可能,恶意脚本可以使用您概述的脚本来访问用户先前在input字段中input数据,例如信用卡号或电子邮件地址。 This would be a pretty bad security hole. 这将是一个非常糟糕的安全漏洞。

Sorry about that. 对于那个很抱歉。 Perhaps you could get away with just looping through the input values your want to test and calling $('#medID').val(testValueHere) instead? 也许您可以通过遍历要测试的输入值并调用$('#medID').val(testValueHere)$('#medID').val(testValueHere)

The jQuery UI team do something similar for their own unit tests for autocomplete . jQuery UI团队为自己的自动完成单元测试做类似的事情。 See this answer for more details. 有关更多详细信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM