【问题标题】:Identify submit button click of UIWebview识别 UIWebview 的提交按钮点击
【发布时间】:2017-02-09 19:59:16
【问题描述】:

我有一个带有多个按钮的 UIWebView 页面。需要识别UIWebView.的哪个按钮被点击

UIWebview 加载内容的页面源:

        <form action="settings_personal.php" data-ajax="false" method="post">   
            <input type="submit" name ="btn_settings_personal" value=" Goals (Personal data) " data-icon="star" data-theme="g" />
        </form> 

        <form action="settings_global.php" data-ajax="false" method="post"> 
            <input type="submit" name ="btn_settings_global" value=" Settings " data-icon="gear" data-theme="g" />
        </form>
        <form action="" data-ajax="false" method="post">    
            <input type="submit" name ="btn_web_access" value=" Web Plattform Access " data-icon="plus" data-theme="h" />
        </form>     

我需要识别为“btn_web_access”命名按钮单击的按钮???

我已经实现了以下:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{

 NSString* clicked = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('btn_web_access').click();"];

    NSLog(@"CLICK %@",clicked);

}

但它没有用..提前谢谢。

【问题讨论】:

  • 您的 stringByEvaluatingJavaScriptFromString 调用只会触发一个新的按钮单击事件。这似乎没有用,因为您要问的是如何识别单击了哪个按钮,而不是如何以编程方式单击按钮。看看我的回答是否对你有帮助。

标签: iphone uiwebview uibutton getelementbyid


【解决方案1】:

只需使用请求的URL属性来区分加载请求的目标即可:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{

    NSLog(@"url: %@", [[request URL] absoluteString]);

    return YES;

}

根据调用的 URL,您可以知道您当前显示的网页的哪个按钮(甚至是任何链接)被点击了。然后执行所需的操作,不要忘记返回一个 BOOL 值,指示是否实际加载请求的 URL。

在 Swift 中你可能有

func webView(webView:UIWebView,
          shouldStartLoadWithRequest request:NSURLRequest,
          navigationType:UIWebViewNavigationType) -> Bool
    {
    if (navigationType == .FormSubmitted)
        {
        print("was the 'submit' form")
        }

    let u = request.mainDocumentURL
    print("local or remote url was " ,u)

    return true // allow the form to in fact send the form
    }

【讨论】:

  • 但是 Alex 的网页没有任何动作,请检查下面的
  • 只需在 action 参数中设置任意链接,这将帮助您识别您的按钮。当用户单击此按钮时,只需在 shouldStartLoadWithRequest 中返回一个 NO。因此,此点击不会导致 webview 中的实际页面加载。
  • Alex 感谢您提供任何其他选项,例如识别 UIWebview 的 Clicked 按钮的名称。
  • 另一种方法类似: 1) 在 HTML 中,不要在单击按钮时直接提交表单。相反,调用一个 javascript 函数,该函数将按钮标识符保存在全局变量中 然后在必要时提交表单。 2) 通过stringByEvaluatingJavaScriptFromString 获取shouldStartLoadWithRequest 方法内部的当前javascript 按钮标识符值。但是您仍然需要为每个表单设置一个操作参数。
  • Alex 我有另一个选项来获取 UIWebview 按钮的值,即更容易识别的名称...通过以下代码 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:( NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSString* value1 = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('btn_web_access')[0].value"]; NSLog(@"用户名:%@",value1); }
【解决方案2】:

使用 HTML 代码进行此操作

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:
    (NSURLRequest *)request navigationType: 
    (UIWebViewNavigationType)navigationType {
        if ([request.URL.scheme isEqualToString:@"inapp"]) {
            if ([request.URL.host isEqualToString:@"capture"]) {
            // do capture action
        }  
        return NO;
     }  

   return YES;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多