Javascript 查找用户选择的输入字段的输入元素ID
我有一个Javascript 查找用户选择的输入字段的输入元素ID,javascript,ios,uiwebview,Javascript,Ios,Uiwebview,我有一个UIWebView,并且从服务器加载了一个文档。web文档有几个文本输入字段。如何确定用户已触摸的字段的ID或名称-已选择。然后我想分配一个值来填充输入字段 我已经读了足够多的书,相信我需要JavaScript,但不知道如何将它与Xcode中的objective c相关联。任何帮助都将不胜感激 谢谢 Ron这里有一个简单实现的快速示例,可以帮助您开始 example.html 希望有帮助 谢谢你的链接,但这只是我问题的一部分。 <script type="text/javacrip
UIWebView
,并且从服务器加载了一个文档。web文档有几个文本输入字段。如何确定用户已触摸的字段的ID或名称-已选择。然后我想分配一个值来填充输入字段
我已经读了足够多的书,相信我需要JavaScript,但不知道如何将它与Xcode中的objective c相关联。任何帮助都将不胜感激
谢谢
Ron这里有一个简单实现的快速示例,可以帮助您开始 example.html
希望有帮助 谢谢你的链接,但这只是我问题的一部分。
<script type="text/javacript">
function populateField(fieldId, fieldText) {
$('#' + fieldId).val(fieldText);
}
var bridgeScheme = 'myapp';
(function($) {
$('#my-textfield').on('focus', function() {
var data = {
'action': 'focus',
'field-id': $(this).attr('id')
};
var encodedData = encodeURIComponent(JSON.stringify(data));
$('#iframe').attr('src', bridgeScheme + encodedData);
});
})(jQuery)
</script>
<form id="my-form">
<div>
<input type="text" id="my-textfield">
</div>
</form>
<iframe src="" id="iframe"></iframe>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// This needs to be the same value as used in the javascript
NSString *bridgeScheme = @"myapp";
// That's where we capture the request if the request's scheme matches
if ([request.URL.scheme isEqualToString:bridgeScheme])
{
// Extract the part of the request url that contains the data we need
NSString *dataString = [request.URL.absoluteString substringFromIndex:bridgeScheme.length + 1];
// The data was URL encoded
dataString = [dataString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Here we transform the JSON string into a JSON object (dictionary)
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Extract the field id from the dictionary
NSString *fieldId = dataDictionary[@"field-id"];
// Call the javascript method on the webview
NSString *populateFieldJS = [NSString stringWithFormat:@"populateField('%@', '%@')", fieldId, @"Whatever text you want to put in there"];
[webView stringByEvaluatingJavaScriptFromString:populateFieldJS];
return NO;
}
return YES;
}