【问题标题】:Create dynamic checkbox in UIWebView在 UIWebView 中创建动态复选框
【发布时间】:2015-04-16 15:12:26
【问题描述】:

我正在尝试在 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 中没有复选框。请帮帮我。

【问题讨论】:

    标签: javascript html ios objective-c uiwebview


    【解决方案1】:

    如果你想在 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文件..修改如下

    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]];
    
        }
    

    希望对你有帮助...

    【讨论】:

    • 还是空白的 uiwebview :(
    【解决方案2】:

    我已经解决了:

       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];
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2021-12-21
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多