the following code, there are two groups of pictures, user1-image and user2-image, each have three pictures. Now each group can only select one picture, which means that after selecting a picture, you have to traverse the other pictures in this group and restore the other selected picture classes. What do you do? (restore with $(this). Attr ("class", "user-img1 or 2");)) Note that imgList is an array of the names of the last two pictures selected, so the dom is updated, and the array is also updated. Finally, choose one for each group of pictures, that is, two pictures
.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>test</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="panel panel-default">
<!-- <h3>{question}</h3> -->
<div class="panel-body">
<div class="row" style="height:100%;background: -sharpEEE;">
<div style="float: left;">
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img1" data-name="img1" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img1" data-name="img2" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img1" data-name="img3" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
</div>
<div style="float: left;">
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img2" data-name="img1" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img2" data-name="img2" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
<div style="width: 300px;float: left;margin-left: 3px">
<img class="user-img2" data-name="img3" src="file:///Users/1/Desktop/timg.jpeg" style="width:100%" />
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
$(".questionClass>input[type="text"]").attr("readonly", true);
// var d1 = $(".user-img1");
// console.log(d1);
$(document).delegate(".user-img1", "click", function () {
$(this).attr("class", "selected");
console.log($(this));
var textValue = $(".questionClass>input[type="text"]").val();
var imgList = textValue.split(",");
if (textValue == undefined || textValue == "") {
imgList = new Array();
}
imgList.push($(this).data("name"));
$(".questionClass>input[type="text"]").val(imgList.join(","));
});
$(document).delegate(".user-img2", "click", function () {
$(this).attr("class", "selected");
var textValue = $(".questionClass>input[type="text"]").val();
var imgList = textValue.split(",");
if (textValue == undefined || textValue == "") {
imgList = new Array();
}
imgList.push($(this).data("name"));
$(".questionClass>input[type="text"]").val(imgList.join(","))
});
$(document).delegate(".selected", "click", function () {
$(this).attr("class", "user-img1");
var imgList = $(".questionClass>input[type="text"]").val().split(",");
var index = $.inArray($(this).data("name"), imgList);
if (index > -1) {
imgList.splice(index, 1);
}
$(".questionClass>input[type="text"]").val(imgList.join(","))
});
$(document).delegate(".selected", "click", function () {
$(this).attr("class", "user-img2");
var imgList = $(".questionClass>input[type="text"]").val().split(",");
var index = $.inArray($(this).data("name"), imgList);
if (index > -1) {
imgList.splice(index, 1);
}
$(".questionClass>input[type="text"]").val(imgList.join(","))
});
var anwser = $(".answers").attr("data-pre_answer");
if (anwser != null && anwser != "" && anwser != undefined) {
var json = jQuery.parseJSON(anwser);
if (json["text"] != null) {
$.each(json["text"].split(","), function (index, element) {
$("img[data-name="" + element + ""]").attr("class", "selected");
});
$(".questionClass>input[type="text"]").val(json["text"]);
}
}
});
</script>
<style type="text/css">
.selected {
border: solid 10px red;
}
</style>