更新CSV文件以记录使用toggleClass jQuery函数实现的用户选择图像的方法是:
<form id="imageForm">
<img src="image1.jpg" class="toggle-image" data-image-id="1">
<img src="image2.jpg" class="toggle-image" data-image-id="2">
<img src="image3.jpg" class="toggle-image" data-image-id="3">
</form>
$(document).ready(function() {
$('.toggle-image').on('click', function() {
$(this).toggleClass('selected');
});
});
function createCSV() {
var csvContent = "data:text/csv;charset=utf-8,";
var rows = [];
$('.selected').each(function() {
var imageId = $(this).data('image-id');
var row = [imageId];
rows.push(row);
});
rows.forEach(function(rowArray) {
var row = rowArray.join(",");
csvContent += row + "\r\n";
});
var encodedURI = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedURI);
link.setAttribute("download", "image_selection.csv");
document.body.appendChild(link);
link.click();
}
$('#imageForm').on('submit', function(e) {
e.preventDefault();
createCSV();
});
这样,当用户在表单中切换图像选择时,所选图像的信息将被记录在CSV文件中,以便进一步处理和分析。
领取专属 10元无门槛券
手把手带您无忧上云