Javascript 数组包含值

发布于 2024-12-19 03:16:25 字数 2563 浏览 1 评论 0原文

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

我可以在 JavaScript 中使用 array contains ,或者有更好的方法吗?

我的尝试:

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

On page load, I return a JSON array:

{
    "src":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","TDE2pWgIfa.jpg","tUtuDx1BEf.jpg"],
    "checked":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","tUtuDx1BEf.jpg"]
}
  • The src contains all the source names of image and checked is the checked type of check boxes.
  • The checked defines which image is checked (every image has a check box).

I need the final HTML like this:

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

I can use array contains in JavaScript, or is there a better way?

My attempt:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我早已燃尽 2024-12-26 03:16:25

您可以使用 Array 的 indexOf 方法

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

注意,indexOf 方法不存在在 IE6 中,但 Mozilla 在 同一页面

You can use indexOf method of Array

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

Note that indexOf method is absent in IE6, but there is an implementation available by Mozilla on the same page.

眸中客 2024-12-26 03:16:25

这是我使用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);
})

Here's my complete solution using 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);
})
回心转意 2024-12-26 03:16:25

更好的方法是替换

{"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 设置为空字符串或“checked”,具体取决于复选框是否被选中。

A better approach would be to replace

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

with

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

for instance.

Then you could just write

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

The only thing different between the two strings you make is the attribute checked (which should really be checked="checked" if you want to follow standards), so I'd prefer setting a variable checked to either an empty string or "checked" depending on whether the checkbox is checked or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文