0

我有一个 UIWebView,我有一个像这样的简单表单:

    <form action="http://message/">Your Email: <input id="email" /><br />
        <input type="submit" value="Submit" />
    </form>

现在没有 JS 验证,表单只是在某处提交,我不知道如何获取字段值。我的控制器中只有这样:

- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{   
    if ([request.URL.scheme isEqualToString:@"message"]) 
    {
        // RIGHT NOW THIS DOESNT WORK...HOW DO I GET IT RIGHT? :)
        NSString *useremail = [theWebView stringByEvaluatingJavaScriptFromString:@"getEmail()"];
        NSLog(useremail);

    }  

    return YES;
}

我不确定如何让控制器查看字段值以及如何验证 js 中的字段值。

请告诉我如何最好地做到这一点。

我还尝试创建一个 JavaScript 函数来简单地点击请求 url(这就是我真正需要的),但这不是发送请求 url:

    <script type="text/javascript">
    function test () 
    {
        // Het the params.
        var name = escape(document.getElementById("name").value);
        var email = escape(document.getElementById("email").value);
        var phone = escape(document.getElementById("phone").value);


        var url="http://www.myurl.com?platform=ios&name="+name+"&email="+email+"&phone="+phone;
        alert(url);




        var ajaxObj = createXMLHttp();
        //var url = 'myrequest.php';
        ajaxObj.open("GET", url, true);
        ajaxObj.onreadystatechange = function() 
        {
            ajaxObj.processRequest();
        }
            ajaxObj.send(null);

            ajaxObj.processRequest = function() 
            {
              if (this.readyState == 4) 
              {
                // got response
              }
            }


        alert("1");
        var req ;

        // Browser compatibility check                  
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {

            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {

                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }

        }


        var req = new XMLHttpRequest();
        req.open("GET", url ,true);
        req.onreadystatechange = function () 
        {
            //document.getElementById('divTxt').innerHTML = "Contents : " + req.responseText;
        }

        req.send(null);  

       return false;
    }
        </script>

谢谢!

4

1 回答 1

3

像这样的东西怎么样:

- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{   
    if ([request.URL.scheme isEqualToString:@"message"]) 
    {
        NSString *useremail = [theWebView stringByEvaluatingJavaScriptFromString:@"escape(document.getElementById(\"email\").value);"];
        NSLog(useremail);

    }  

    return YES;
}
于 2012-10-11T16:33:30.417 回答