iphone - 从uiwebview文本域获取数据

nik*_*nik 24 html javascript objective-c uiwebview ios

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

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

谢谢

Uda*_*ala 48

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

示例:如果您的html是:

<html>
  <body>
      <input id="myId" type="text" value="TextValue"/>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以下代码可以获取文本字段的值:

   NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];
Run Code Online (Sandbox Code Playgroud)

  • @niki我认为所有内容都具有在Webview中执行javascript的能力。在上一个示例中,要在文本字段中设置值,您可以执行[webView stringByEvaluatingJavaScriptFromString:@“ document.getElementById('myId')。value ='newValue'”]。同样,您可以使用javascript或webview中的加载方法重新加载。 (2认同)