我有一个带有文本字段和提交按钮的网页。该网页是在 UIWebView 中加载的。
我如何以编程方式按下该网页的提交按钮?
或者,我如何以编程方式使网页内的文本字段成为第一响应者并显示软键盘?只需以编程方式重新创建相同的行为,就好像用户触摸了网页上的文本字段一样。
我想假设该页面的来源未知。
这不是 UITextField,它只是 UIWebView 中显示的网页内的一个。
提前致谢。
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.
发布评论
评论(2)
如果你知道页面的来源,你可以使用UIWebView的stringByEvaluatingJavascriptFromString。例如:
NSString *focusTextField = @"document.getElementById('textFieldID').focus();";
[webView stringByEvaluatingJavascriptFromString:focusTextField];
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];
我不确定我的问题是否清楚,但这是对我有用的答案:
I'm not sure if my question was clear, but this was the answer that worked for me: