Javascript iphone - 从 uiwebview 文本字段获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5908775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:27:26  来源:igfitidea点击:

iphone - Getting data from uiwebview textfield

javascripthtmlobjective-ciosuiwebview

提问by nik

Am developing an app which load a HTML page into uiwebview - that html file contains text-filed and drop-down list, text-boxes etc..

正在开发一个将 HTML 页面加载到 uiwebview 的应用程序 - 该 html 文件包含文本文件和下拉列表、文本框等。

Here How to get values which user has entered in that text-filed and answers selected from drop-down(picker).

这里如何获取用户在该文本文件中输入的值以及从下拉列表(选择器)中选择的答案。

Thanks

谢谢

回答by Udayakumar Rayala

You can use [UIWebView stringByEvaluatingJavascriptFromString:]method and get the values by executing the javascript.

您可以使用[UIWebView stringByEvaluatingJavascriptFromString:]方法并通过执行 javascript 来获取值。

Example: If your html is:

示例:如果您的 html 是:

<html>
  <body>
      <input id="myId" type="text" value="TextValue"/>
  </body>
</html>

Following code can get you the value of the text field:

以下代码可以让您获得文本字段的值:

   NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];

回答by iceydee

You can either poll a javascript function inside the page (using UIWebView stringByEvaluatingJavascriptFromString:).

您可以轮询页面内的 javascript 函数(使用UIWebView stringByEvaluatingJavascriptFromString:)。

Or you could call an objective-c method directly from the page using a little hack: UIWEBVIEW SECRETS - PART3 - HOW TO PROPERLY CALL OBJECTIVEC FROM JAVASCRIPT
(scroll down to the bottom for github-link).

或者,您可以使用一个小技巧直接从页面调用 Objective-c 方法:UIWEBVIEW SECRETS - PART3 - HOW TO PROPERLY CALL OBJECTIVEC from JAVASCRIPT
(向下滚动到 github-link 的底部)。

回答by Zaid Pathan

If HTML response is this,

如果 HTML 响应是这样的,

<html>
  <body>
      <input id="myId" type="hidden" value="TextValue"/>
  </body>
</html>

So you can access like this in Swift,

所以你可以在Swift 中像这样访问,

  var id:String = IBwebView.stringByEvaluatingJavaScriptFromString("document.getElementById('myId').value")!
  println(id)