简体   繁体   中英

jQuery Auto-complete - Force Selection/Testing

I have a simpe jQuery autocomplete input box with the ID 'medID'

$('#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.

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. 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. 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?

The jQuery UI team do something similar for their own unit tests for autocomplete . See this answer for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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