programmatically handling a text field within a uiwebview

烈酒焚心 提交于 2019-12-23 04:52:26

问题


I have a webpage with a text field and a submit button. That webpage is loaded within a UIWebView.

How would I programmatically press the submit button for that webpage?

Alternatively, how would I programmatically make the text field within the webpage the first responder and display the soft keyboard? Just programmatically re-create the same behavior as if the user had touched the text field on the webpage.

I would like to make the assumption that the source of the page is unknown.

This is not a UITextField it is just a within the webpage that is displayed in a UIWebView.

Thanks in advance.


回答1:


If you know the source of the page, you can use UIWebView's stringByEvaluatingJavascriptFromString. For example:

NSString *focusTextField = @"document.getElementById('textFieldID').focus();";
[webView stringByEvaluatingJavascriptFromString:focusTextField];




回答2:


I'm not sure if my question was clear, but this was the answer that worked for me:

jScriptString = [NSString stringWithFormat:@"var field = document.activeElement;" 
                     "field.value='%@';", symbol.data];
    [myWebView stringByEvaluatingJavaScriptFromString:jScriptString];
    jScriptString = [NSString stringWithFormat:@"document.activeElement.form.submit();"];
    [myWebView stringByEvaluatingJavaScriptFromString:jScriptString];


来源:https://stackoverflow.com/questions/5616599/programmatically-handling-a-text-field-within-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!