Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在单击复选框后获取src属性值?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在单击复选框后获取src属性值?

Javascript 如何在单击复选框后获取src属性值?,javascript,jquery,Javascript,Jquery,我有一个复选框,上面有一个替换图像。我试图做的是单击复选框,获取图像的src属性值,并将其放置在文本区域中 HTML <div class="thumbnail"> <input type="checkbox" name="thing_5" value="valuable" id="thing_5"> <label for="thing_5"> <img class="img-responsive" src="https://upload

我有一个复选框,上面有一个替换图像。我试图做的是单击复选框,获取图像的src属性值,并将其放置在文本区域中

HTML

<div class="thumbnail">
  <input type="checkbox" name="thing_5" value="valuable" id="thing_5">
  <label for="thing_5">
    <img class="img-responsive" src="https://upload.wikimedia.org/wikipedia/commons/a/ac/U.S._Marines_in_Operation_Allen_Brook_(Vietnam_War)_001.jpg">
  </label>
</div>
<textarea id='txtarea'></textarea>
CSS

textarea{width:100%;height:200px;}
input[type=checkbox] {
    display: none;
}

input[type=checkbox] + label {
    background: #999;
    display: inline-block;
    padding: 0;
}

input[type=checkbox]:checked + label {
    border: 10px solid grey;
    padding: 0;
}
目前,我在
文本区域内得到
未定义的

JsFiddle

由于
内的
.nextAll()
无法工作。您需要使用
.next()
.find()

片段

$(文档).ready(函数(){
var tempValue=“”;
$('.thumbnail:checkbox').change(函数(){
tempValue+=$(this.next(“label”).find(“img”).attr(“src”);
$('textarea').html(tempValue);
});
});
textarea{
宽度:100%;
高度:200px;
}
输入[类型=复选框]{
显示:无;
}
输入[类型=复选框]+标签{
背景:#999;
显示:内联块;
填充:0;
}
输入[类型=复选框]:选中+标签{
边框:10px纯灰;
填充:0;
}

由于
.nextAll()
内部,因此
.nextAll()无法工作。您需要使用
.next()
.find()

片段

$(文档).ready(函数(){
var tempValue=“”;
$('.thumbnail:checkbox').change(函数(){
tempValue+=$(this.next(“label”).find(“img”).attr(“src”);
$('textarea').html(tempValue);
});
});
textarea{
宽度:100%;
高度:200px;
}
输入[类型=复选框]{
显示:无;
}
输入[类型=复选框]+标签{
背景:#999;
显示:内联块;
填充:0;
}
输入[类型=复选框]:选中+标签{
边框:10px纯灰;
填充:0;
}


像这样:像这样:嘿,谢谢,我马上就接受。我是否可以在文本区域中添加/删除选中的项目?如果我有更多的图像/复选框,例如@rob.m,你可能需要重做整个循环。那是最好的办法。你想要一个不同的解决方案吗?是的,也许这不是最好的解决方案,如果你能帮忙的话,我会的thankful@rob.m做吧。几分钟。嘿,谢谢,我马上就接受。我是否可以在文本区域中添加/删除选中的项目?如果我有更多的图像/复选框,例如@rob.m,你可能需要重做整个循环。那是最好的办法。你想要一个不同的解决方案吗?是的,也许这不是最好的解决方案,如果你能帮忙的话,我会的thankful@rob.m做吧。几分钟。
textarea{width:100%;height:200px;}
input[type=checkbox] {
    display: none;
}

input[type=checkbox] + label {
    background: #999;
    display: inline-block;
    padding: 0;
}

input[type=checkbox]:checked + label {
    border: 10px solid grey;
    padding: 0;
}
tempValue += $(this).next("label").find("img").attr("src");