这是我的代码
<textarea id="summernote<?php echo $u->id ?>" name="isi" placeholder="Tulis di sini" style="width: 100%; height: 100px !important; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">
我把我的JavaScript放在文本区域下
<script type="text/javascript">
var id1="#summernote<?php echo $u->id ?>"
$(document).ready(function(){
$(id1).summernote({
height: "300px",
callbacks: {
onImageUpload: function(image) {
uploadImage(image[0]);
},
onMediaDelete : function(target) {
deleteImage(target[0].src);
}
}
});
function uploadImage(image) {
var data = new FormData();
data.append("image", image);
$.ajax({
url: "<?php echo site_url('trans/upload_image')?>",
cache: false,
contentType: false,
processData: false,
data: data,
type: "POST",
success: function(url) {
$(id1).summernote("insertImage", url);
},
error: function(data) {
console.log(data);
}
});
}
function deleteImage(src) {
$.ajax({
data: {src : src},
type: "POST",
url: "<?php echo site_url('trans/delete_image')?>",
cache: false,
success: function(response) {
console.log(response);
}
});
}
});
</script>
我想用随机 id 显示 summernote,因为当我的文本区域中只有一个 id 时,它只是一个 id。所以我想给它随机 ID。但是我的代码不起作用,我不知道如何在javascript中实现。
改用类并遍历该类中的所有元素以初始化summerNote
实例。还要根据需要修改函数以传入编辑器元素实例
.HTML
<textarea class="editor" id="summernote<?php echo $u->id ?>" ></textarea>
JS(缩写(
$('.editor').each(function(){
// `this` is the current textarea element
const $textarea = $(this);
// init plugin for specific textarea instance
$textarea.summernote({
....
callbacks: {
onImageUpload: function(image) {
uploadImage($textarea, image[0]);
// ^^ which editor element
},
....
}
});
});
function uploadImage($testarea, image) {
// ^^ textarea object argument
.....
$.ajax({
.....
success: function(url) {
$testarea.summernote("insertImage", url);
// ^^ specific jQuery textarea instance
},
.....
});