IOS带单选按钮(html5)

时间:2014-02-07 10:14:00

标签: javascript ios html5 uiwebview

有可能吗?如果我使用UIWebview调用带有单选按钮的本地html。我可以从html获取数据单选按钮吗?

这是IOS代码

 for (int i = 0; i < [book count]; i++) {
            CGRect frame = self.scrollView.frame;
            frame.origin.x = self.scrollView.frame.size.width * i;
            frame.origin.y = 0;
            frame.size.height = scrollView.frame.size.height;
            frame.size.width = scrollView.frame.size.width;

            subView = [[UIWebView alloc] initWithFrame:frame];
            subView.scalesPageToFit = YES;
            subView.backgroundColor = [UIColor blackColor];
            subView.delegate = self;

            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[book objectAtIndex:i] ofType:@"html" inDirectory:@""]];
            [subView loadRequest:[NSURLRequest requestWithURL:url]];

            subView.scrollView.bounces = NO;

            [scrollView addSubview:subView];
        }
        [scrollView setFrame:CGRectMake(0, 0, scrollView.frame.size.width,  scrollView.frame.size.height)];
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [book count], scrollView.frame.size.height);

        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[book objectAtIndex:currentPage] ofType:@"html" inDirectory:@""]];
        [[scrollView.subviews objectAtIndex:currentPage] loadRequest:[NSURLRequest requestWithURL:url]];

        [scrollView setContentOffset:CGPointMake((scrollView.frame.size.width*currentPage), 0) animated:NO];

        subView.scrollView.bounces = NO;

        [[subView scrollView] setBounces: NO];

和HTML代码

<form name="frm1" method="get" action="p4.html">
<div id="question1">

<p><b>The phenomenon of a disproportionate traffic development indicates ...</b></p>

 <p><input type="radio" name="question1" value="a1"/><label>An increase of traffic in urban and inner city areas.</label></p>

 <p><input type="radio" name=question1" value="a2"/><label>The need for better and more efficient public, as well as freight transportation.</label></p>

 <p><input type="radio" name="question1" value="a3"/><label>Rising challenges on tackling environmental problems due to increasing traffic.</label></p>

 <p><input type="radio" name="question1" value="a4"/><label>All of the above</label></p>

 <br />
 <br /> 
</div>
<div id="next" onclick="Submit()"> </div>
</form>

我可以从html页面获取数据到IOS。我只想在点击单选按钮时显示nslog

1 个答案:

答案 0 :(得分:0)

使用UIWebView委托捕获对表单操作的调用,并对页面执行js调用:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] rangeOfString:@"p4"].location != NSNotFound) {
        NSLog(@"%@", [self.webView stringByEvaluatingJavaScriptFromString:@"$('input[name=\"question1\"]:checked').val();"]);
        return NO;
    }
}