1
<div id="image_div">
<input type="checkbox" />
<img src="1.jpg" />
<input type="checkbox" />
<img src="2.jpg" />
<input type="checkbox" />
<img src="3.jpg" />
<input type="checkbox" />
</div>

在页面加载时,我返回一个 JSON 数组:

{
    "src":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","TDE2pWgIfa.jpg","tUtuDx1BEf.jpg"],
    "checked":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","tUtuDx1BEf.jpg"]
}
  • src包含图像的所有源名称,并且checked是复选框的选中类型。
  • 定义检查哪个图像(checked每个图像都有一个复选框)。

我需要这样的最终 HTML:

<div id="image_div">
<input type="checkbox" />
<img src="lml7x8nJzI.jpg" />   
<input type="checkbox" checked />              //this is a checked one
<img src="TaR7dALIPJ.jpg" />    
<input type="checkbox" checked/>                 //this is a checked one
<img src="TDE2pWgIfa.jpg" />
<input type="checkbox" />                    //this is not
<img src="tUtuDx1BEf.jpg" />     
<input type="checkbox" checked />  //this is a checked one
</div>

我可以array contains在 JavaScript 中使用,还是有更好的方法?

我的尝试:

var initial = "<ul class='gallary'>";
var middle = "";
var end = "</ul>";
var i;

for (i = 0; i < data.src.length; i++)
{
    if (data.checked.contains(data.src[i]))   // How can I do this?
    {
        middle = middle +
            "<li><img src=" +
            "http://localhost/project/user/" +
            "<?php echo $this->session->userdata('username');?>" +
            "/pages/" +
            "<?php echo $this->uri->segment(3);?>" +
            "/images/gallery/" +
            data.src[i] +
            " />" +
            "<br /><input type='checkbox' value=" +
            data.src[i] +
            " checked/></li>";
    }
    else
    {
        middle = middle +
            "<li><img src=" +
            "http://localhost/project/user/" +
            "<?php echo $this->session->userdata('username');?>" +
            "/pages/" +
            "<?php echo $this->uri->segment(3);?>" +
            "/images/gallery/" +
            data.src[i] +
            " />" +
            "<br /><input type='checkbox' value=" +
            data.src[i] +
            " /></li>";
    }
}

var complete = initial + middle + end;
$("#project_gallary_layout").html(complete);
4

3 回答 3

3

您可以使用Array 的indexOf方法

var a = ['a', 'b', 'c'];
if (a.indexOf('a') > -1 ) {
    //element is in array
} else {
    //element is not in array
}

请注意,IE6 中没有 indexOf 方法,但 Mozilla 在同一页面上提供了一个实现。

于 2011-11-30T06:44:45.857 回答
1

更好的方法是替换

{"src":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","TDE2pWgIfa.jpg","tUtuDx1BEf.jpg"],
 "checked":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","tUtuDx1BEf.jpg"]}

{"src":[
    {"src": "lml7x8nJzI.jpg", "checked": true},
    {"src": "TaR7dALIPJ.jpg", "checked": true},
    {"src": "TDE2pWgIfa.jpg", "checked": false},
    {"src": "tUtuDx1BEf.jpg", "checked": true}]
}

例如。

然后你可以写

for(i=0;i<data.src.length;i++) {
    var elem = data.src[i];
    var checked = "";
    if (elem.checked)
        checked = "checked";
    middle = middle + 
        "<li><img src=" +
        "http://localhost/project/user/" +
        "<?php echo $this->session->userdata('username');?>" +
        "/pages/" +
        "<?php echo $this->uri->segment(3);?>" +
        "/images/gallery/" +
        elem.src +
        " />" +
        "<br /><input type='checkbox' value=" +
        elem.src +
        " " + 
        checked +
        "/></li>";
    }
}

您制作的两个字符串之间唯一不同的是属性checked(如果您想遵循标准,应该确实checked="checked"如此),所以我更喜欢将变量设置checked为空字符串或“选中”,具体取决于复选框是否被选中或不。

于 2011-11-30T06:44:38.893 回答
1

这是我使用 indexOf 的完整解决方案

$(function(){
  var url = 'http://localhost/project/user/<?php echo $this->session->userdata('username'); ?>/pages/<?php echo $this->uri->segment(3); ?>/images/gallery/'
    , complete = '<ul class="gallery">'

  data.src.forEach(function(src){
    complete += '<li><img src="' + url + src + '"><input type="checkbox" value="' + src + '"'
    if(data.checked.indexOf(src) > -1){
      complete += ' checked'
    }
    complete += '></li>'
  })

  $("#project_gallary_layout").html(complete);
})
于 2011-11-30T06:47:07.033 回答