【问题标题】:How to get id in javascript from php to show data如何从 php 获取 javascript 中的 id 以显示数据
【发布时间】:2020-10-18 02:12:38
【问题描述】:

这是我的代码

 <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 放在 textarea 下

<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,因为当我的 textarea 中只有一个 id 时,它只适用于一个。所以我想给它随机的ID。但是我的代码不起作用,我不知道如何在javascript中实现。

【问题讨论】:

标签: javascript php ajax codeigniter summernote


【解决方案1】:

改用一个类并遍历该类中的所有元素来初始化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
            },
            .....
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多