英文:
how to delete specific image using jquery in sortable jquery
问题
我有一个HTML中的图像部分,其中图像以网格形式显示,用户可以拖动和重新排列,根据重新排列,图像名称存储在隐藏的输入框中,这部分代码如下:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
$(function () {
$("#imageListId").sortable({
update: function (event, ui) {
getIdsOfImages();
}, //end update
});
});
function getIdsOfImages() {
var values = [];
$(".listitemClass").each(function (index) {
values.push($(this).attr("id").replace("im", ""));
});
$("#outputvalues").val(values);
}
<!-- 语言: lang-css -->
/* 图像尺寸 */
.listitemClass img {
height: 200px;
width: 100%;
}
/* imagelistId 样式 */
#imageListId {
margin: 0;
padding: 0;
list-style-type: none;
}
#imageListId div {
margin: 0 4px 4px 4px;
padding: 0.4em;
display: inline-block;
}
/* 输出顺序样式 */
#outputvalues {
margin: 0 2px 2px 2px;
padding: 0.4em;
padding-left: 1.5em;
width: 250px;
border: 2px solid dark-green;
background: gray;
}
.listitemClass {
border: 1px solid #006400;
width: 25%;
}
.height {
height: 10px;
}
<!-- 语言: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<b>拖放图像以排列</b>
<div class="height"></div><br>
<div id="imageListId" style="display:flex">
<div id="im1" class="listitemClass">
<img src="https://picsum.photos/id/237/200/300" alt="">
</div>
<div id="im2" class="listitemClass">
<img src="https://picsum.photos/id/230/200/300" alt="">
</div>
<div id="im3" class="listitemClass">
<img src="https://picsum.photos/id/235/200/300" alt="">
</div>
<div id="im4" class="listitemClass">
<img src="https://picsum.photos/id/233/200/300" alt="">
</div>
</div>
<form action="" method="post" enctype="multipart/form-data" style="margin-top:3%">
<div id="outputDiv">
<input id="outputvalues" type="hidden" value="" name="nname" />
<input type="hidden" value="<?=$nname1?>" name="nname1" />
</div>
<button type="submit" name="editcategory" class="btn btn-primary">保存</button>
</form>
<!-- 结束代码片段 -->
我想从此网格中删除某些特定图像,请告诉我如何实现这一点,提前感谢。
英文:
I have an image section in html, where images are shown as grid and user can drag and rearrange, according to the rerrangement the image names are stored in the hidden input box, this is working fine, my code is like below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function () {
$("#imageListId").sortable({
update: function (event, ui) {
getIdsOfImages();
}, //end update
});
});
function getIdsOfImages() {
var values = [];
$(".listitemClass").each(function (index) {
values.push($(this).attr("id").replace("im", ""));
});
$("#outputvalues").val(values);
}
<!-- language: lang-css -->
/* image dimension */
.listitemClass img {
height: 200px;
width: 100%;
}
/* imagelistId styling */
#imageListId {
margin: 0;
padding: 0;
list-style-type: none;
}
#imageListId div {
margin: 0 4px 4px 4px;
padding: 0.4em;
display: inline-block;
}
/* Output order styling */
#outputvalues {
margin: 0 2px 2px 2px;
padding: 0.4em;
padding-left: 1.5em;
width: 250px;
border: 2px solid dark-green;
background: gray;
}
.listitemClass {
border: 1px solid #006400;
width: 25%;
}
.height {
height: 10px;
}
<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<b>Drag and drop images to arrange</b>
<div class="height"></div><br>
<div id="imageListId" style="display:flex">
<div id="im1" class="listitemClass">
<img src="https://picsum.photos/id/237/200/300" alt="">
</div>
<div id="im2" class="listitemClass">
<img src="https://picsum.photos/id/230/200/300" alt="">
</div>
<div id="im3" class="listitemClass">
<img src="https://picsum.photos/id/235/200/300" alt="">
</div>
<div id="im4" class="listitemClass">
<img src="https://picsum.photos/id/233/200/300" alt="">
</div>
</div>
<form action="" method="post" enctype="multipart/form-data" style="margin-top:3%">
<div id="outputDiv">
<input id="outputvalues" type="hidden" value="" name="nname" />
<input type="hidden" value="<?=$nname1?>" name="nname1" />
</div>
<button type="submit" name="editcategory" class="btn btn-primary">SAVE</button>
</form>
<!-- end snippet -->
i want to delete some specific image from this grid, please tell me how do i accomplish this, thanks in advance
答案1
得分: 2
你需要重置可排序组件:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
const getIdsOfImages = () => {
const values = $('.listitemClass').map(function() {
return this.id.replace('im', '')
}).get();
$('#outputvalues').val(values);
};
const init = () => {
$('#imageListId').sortable({
update: getIdsOfImages
});
getIdsOfImages();
}
$('.delete').on('click', function() {
$(this).closest('.listitemClass').remove();
init();
})
init();
});
<!-- language: lang-css -->
/* 图片尺寸 */
.listitemClass img {
height: 200px;
width: 100%;
}
/* imagelistId 样式 */
#imageListId {
margin: 0;
padding: 0;
display:flex;
}
#imageListId div {
margin: 0 4px 4px 4px;
padding: 0.4em;
display: inline-block;
}
/* 输出顺序样式 */
#outputvalues {
margin: 0 2px 2px 2px;
padding: 0.4em;
padding-left: 1.5em;
width: 250px;
border: 2px solid dark-green;
background: gray;
}
.listitemClass {
border: 1px solid #006400;
width: 25%;
}
.height {
height: 10px;
}
<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<b>拖拽图像以排列</b>
<div class="height"></div><br>
<div id="imageListId">
<div id="im1" class="listitemClass">
<img src="https://picsum.photos/id/236/200/300" alt="Image 1">
<button type="button" class="btn btn-primary delete">X</button>
</div>
<div id="im2" class="listitemClass">
<img src="https://picsum.photos/id/237/200/300" alt="Image 2">
<button type="button" class="btn btn-primary delete">X</button>
</div>
<div id="im3" class="listitemClass">
<img src="https://picsum.photos/id/238/200/300" alt="Image 3">
<button type="button" class="btn btn-primary delete">X</button>
</div>
</div>
<form action="" method="post" enctype="multipart/form-data" style="margin-top:3%;">
<div id="outputDiv">
<input id="outputvalues" type="text" value="" name="nname" />
<input type="hidden" value="1" name="nname1" />
</div>
</form>
<!-- end snippet -->
这是你提供的代码的翻译。如果有其他需要,请随时告诉我。
英文:
You need to reset the sortable:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
const getIdsOfImages = () => {
const values = $('.listitemClass').map(function() {
return this.id.replace('im', '')
}).get();
$('#outputvalues').val(values);
};
const init = () => {
$('#imageListId').sortable({
update: getIdsOfImages
});
getIdsOfImages();
}
$('.delete').on('click', function() {
$(this).closest('.listitemClass').remove();
init();
})
init();
});
<!-- language: lang-css -->
/* image dimension */
.listitemClass img {
height: 200px;
width: 100%;
}
/* imagelistId styling */
#imageListId {
margin: 0;
padding: 0;
display:flex;
}
#imageListId div {
margin: 0 4px 4px 4px;
padding: 0.4em;
display: inline-block;
}
/* Output order styling */
#outputvalues {
margin: 0 2px 2px 2px;
padding: 0.4em;
padding-left: 1.5em;
width: 250px;
border: 2px solid dark-green;
background: gray;
}
.listitemClass {
border: 1px solid #006400;
width: 25%;
}
.height {
height: 10px;
}
<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<b>Drag and drop images to arrange</b>
<div class="height"></div><br>
<div id="imageListId">
<div id="im1" class="listitemClass">
<img src="https://picsum.photos/id/236/200/300" alt="Image 1">
<button type="button" class="btn btn-primary delete">X</button>
</div>
<div id="im2" class="listitemClass">
<img src="https://picsum.photos/id/237/200/300" alt="Image 2">
<button type="button" class="btn btn-primary delete">X</button>
</div>
<div id="im3" class="listitemClass">
<img src="https://picsum.photos/id/238/200/300" alt="Image 3">
<button type="button" class="btn btn-primary delete">X</button>
</div>
</div>
<form action="" method="post" enctype="multipart/form-data" style="margin-top:3%">
<div id="outputDiv">
<input id="outputvalues" type="text" value="" name="nname" />
<input type="hidden" value="1" name="nname1" />
</div>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论