0

我有一个简单的 jQuery 自动完成输入框,ID 为“medID”

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

我使用上面的代码在框中输入一个值,触发焦点,然后按下键进行搜索。

它返回 1-5 个值。

我如何让它自动选择建议值之一?

我努力了:

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

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

但是建议的自动完成值是输入框的直接孩子吗?

这样我就可以在不需要用户的情况下测试我系统的这一部分。

4

2 回答 2

1

I'm not 100% certain, but I think browsers prohibit this. 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, e.g. credit card numbers or email addresses, using a script just like the one you outlined. 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?

于 2013-08-08T08:35:34.543 回答
0

jQuery UI 团队为他们自己的自动完成单元测试做了类似的事情。有关更多详细信息,请参阅此答案

于 2014-03-02T15:41:16.713 回答