任何人都请帮助我,我怎样才能得到所有的 textFields 值(用他们的“名字”“id”) 在加载的 UIWebView 页面中。
或者请提供一个链接,该链接描述了要传入的所有 jQuery 字符串
"stringByEvaluatingJavaScriptFromString"
方法作为参数。
最佳答案
这是您应该使用的 JavaScript 代码:
(function(){
var i, tf, textFields = document.getElementsByTagName("input"), result = [];
for (i = 0; tf = textFields[i]; i++) {
if (tf.type === "text" || tf.type === "search")
result.push({
"type": tf.type,
"name": tf.name,
"id": tf.id,
"value": tf.value
});
}
return JSON.stringify(result);
})();
您可能会发现想要添加其他输入字段类型。
这是一个 Objective-C 代码示例:
- (void)getValuesButtonTapped:(id)sender {
NSString *resultStr = [self.webView stringByEvaluatingJavaScriptFromString:
@"(function(){"
"var i, tf, textFields = document.getElementsByTagName(\"input\"), result = [];"
"for (i = 0; tf = textFields[i]; i++) {"
"if (tf.type === \"text\" || tf.type === \"search\")"
"result.push({\"type\": tf.type, \"name\": tf.name, \"id\": tf.id, \"value\": tf.value});"
"}"
"return JSON.stringify(result);"
"})();"
];
NSArray *result = [NSJSONSerialization JSONObjectWithData:[resultStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSLog(@"result: %@", result);
}
当您点击“获取值...”按钮时,您会收到以下日志消息:
result: ({
id = "lst-ib";
name = q;
type = search;
value = "hello world";
},
{
id = "";
name = "";
type = text;
value = "";
},
{
id = "";
name = "";
type = text;
value = "";
})
关于jquery - 在 objective-c 中访问 WebView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22687251/