【发布时间】:2022-01-07 10:34:24
【问题描述】:
我有这个 javascript
<script>
$(document).ready(function() {
$('.summernote1').summernote({
placeholder: 'start typing',
lang: 'en-GB',
height: 300, // set editor height
minHeight: null, // set minimum height of editor
maxHeight: null, // set maximum height of editor
focus: true, // set focus to editable area after initializing summernote
callbacks: {
onImageUpload: function(files) {
for (var i = 0; i < files.length; i++) {
send(files[i]);
}
}
}
});
});
function send(file) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.responseText);
const urls = response.url;
urls.forEach(imgurls);
function imgurls(item) {
$('.summernote1').summernote('insertImage', item);
}
}
};
var data = new FormData();
data.append('files[]', file);
xhttp.open("POST", "/admin/ans-img-upload", true);
xhttp.send(data);
}
//第二个
$(document).ready(function() {
$('.summernote2').summernote({
placeholder: 'start typing',
lang: 'en-GB',
height: 300, // set editor height
minHeight: null, // set minimum height of editor
maxHeight: null, // set maximum height of editor
focus: true, // set focus to editable area after initializing summernote
callbacks: {
onImageUpload: function(files) {
for (var i = 0; i < files.length; i++) {
send(files[i]);
}
}
}
});
});
function send(file) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.responseText);
const urls = response.url;
urls.forEach(imgurls);
function imgurls(item) {
$('.summernote2').summernote('insertImage', item);
}
}
};
var data = new FormData();
data.append('files[]', file);
xhttp.open("POST", "/admin/ans-img-upload", true);
xhttp.send(data);
}
</script>
然后我有这个 html 代码
<textarea class="form-control summernote1" id="summernote1" name="af_options[]"></textarea>
<textarea class="form-control summernote2" id="summernote2" name="af_options[]"></textarea>
现在如果我想在summernote1上传图片,图片会被插入到summernote2的最后一个textarea中。
请问我该如何解决这个问题,无论如何我可以在所有 textarea 中使用 1 个 javascript 代码而不是重复 javascript 代码,因为我有一个循环多个 textarea 的 foreach。
请帮忙
感谢您的解决方案....
现在如何追加更多字段...我有下面的代码,但它不适用于图像插入
function add_more_additional_field() {
$('#additional_options').append(
'<textarea class="summernote" id="summernote" name="af_options[]" placeholder="Start typing the answers here"></textarea>'
);
$(document).ready(function() {
$([...document.querySelectorAll('#additional_options .summernote')].pop()).summernote();
});
}
【问题讨论】:
-
请不要在已经回答的问题中添加额外的问题。请在网站上提出新问题。
-
@phuzi 完成 [link]stackoverflow.com/questions/70621777/…
标签: javascript function loops foreach summernote