在UIWebView中创建动态复选框

时间:2015-02-16 06:42:09

标签: javascript html ios objective-c uiwebview

我正在尝试在UIWebView中创建动态复选框。我正在使用此代码:

    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"Sudhaadagdudgdada",@"qwertyuiopo",@"asdfghjkl", nil];

    for (int j = 0; j < array.count; j++)
    {
        NSString *html = [NSString stringWithFormat:@"<div id='check'></div><script> str='';   str += '<input type=\"checkbox\" name=\"one_'+i+'\"  value=\"'+%@+'\" onclick=alert(this.value); />'  document.getElementById(\"check\").innerHTML=str;</script>",[array objectAtIndex:j]];

        [self.webview loadHTMLString:[NSString stringWithFormat:@"%@",html] baseURL:nil];

    }

但UIWebView中没有复选框。请帮帮我。

2 个答案:

答案 0 :(得分:0)

如果你想在webview中加载html,你需要为它添加标签。更新你的代码如下

NSString *html = [NSString stringWithFormat:@"<html><head><div id='check'></div><script> str='';   str += '<input type=\"checkbox\" name=\"one_'+i+'\"  value=\"'+%@+'\" onclick=alert(this.value); />'  document.getElementById(\"check\").innerHTML=str;</script></head></html>",[array objectAtIndex:j]];

//或者如果您想加载javascript文件,如您在code..modify中提到的那样

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"Sudhaadagdudgdada",@"qwertyuiopo",@"asdfghjkl", nil];

    for (int j = 0; j < array.count; j++)
    {
        NSString *html = [NSString stringWithFormat:@"<div id='check'></div><script> str='';   str += '<input type=\"checkbox\" name=\"one_'+i+'\"  value=\"'+%@+'\" onclick=alert(this.value); />'  document.getElementById(\"check\").innerHTML=str;</script>",[array objectAtIndex:j]];

        [webView stringByEvaluatingJavaScriptFromString:
        [NSString stringWithFormat:@"%@",html]];

    }

希望它对你有所帮助......

答案 1 :(得分:0)

我已经解决了这个问题:

   NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"'Sudhaadagdudgdada'",@"'qwertyuiopo'",@"'asdfghjkl'", nil];
        NSString *html = [NSString stringWithFormat:@"<div id='check'></div><script> var j; var jsArray = new Array %@; str=''; for(j = 0; j< 3;  j++){ str += '<input type=\"checkbox\" name=\"one_'+j+'\"  value = \"'+jsArray[j]+'\" onclick=\"alert(this.value);\" />'+jsArray[j]+'<br/><br/>'; } document.getElementById(\"check\").innerHTML=str;</script>",array];
    NSLog(@"html..%@",html);

        [self.webview loadHTMLString:html baseURL:nil];
相关问题