简体   繁体   English

使用 Jquery 将下拉值中的文本添加到光标焦点中的 Summernote Textarea

[英]Adding Text from Dropdown value to Summernote Textarea in Cursor focus using Jquery

I'm still stuck but moved 1 step at a time.我仍然卡住,但一次移动 1 步。 I just want to ask on how will I append a dropdown value into a Summernote Textarea.我只是想问一下如何将下拉值附加到 Summernote Textarea 中。 I tried the same solution for appending in a textarea (without summernote integration) which works but seems not working on Summernote.我尝试了相同的解决方案来附加到一个文本区域(没有summernote集成),它有效但似乎不适用于Summernote。 Their website seems to be down as of the moment.他们的网站目前似乎已关闭。

Here is my Set of Codes and Images from Browser console这是我来自浏览器控制台的一组代码和图像

My Button click event to pass dropdown value into textarea:我的按钮单击事件将下拉值传递到 textarea:

$("#btnAddVariable").click(function (e) {
    var eVariable = $("#EmailVariable option:selected").val();

    var $txt = $(".note-editable");
        var caretPos = $txt[0].selectionStart;
        var textAreaTxt = $txt.val();
        var txtToAdd = eVariable;
        $txt.val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos));              
})

My Dropdown and Button:我的下拉菜单和按钮:

<div class="form-group">
    @Html.LabelFor(model => model.EmailVariable, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-6">
        @Html.DropDownListFor(model => model.EmailVariable, new SelectList(ViewBag.emailVariables, "Value", "Text"), "Please Select", htmlAttributes: new { @class = "form-control" })
       @* @Html.DropDownList(model => model.EmailVariable, new SelectList(ViewBag.emailSender, "Value", "Text"), "Please Select", htmlAttributes: new { @class = "form-control" })*@

    </div>
    <div class="col-md-2">
        <input type="button" id="btnAddVariable" value="Add Variable" class="btn btn-primary chosen" />
    </div>
</div>

Text Area with summernote:带有summernote的文本区域:

<div class="form-group">
    @Html.LabelFor(model => model.EmailMessage, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-8">
        <div class="ibox-content no-padding">
            @Html.TextAreaFor(model => model.EmailMessage, new { @class = "summernote", rows = "5", cols = "90" })
        </div>
    </div>
</div>

Browser Console image:浏览器控制台图像:

图片

I hope someone can help me with this.我希望有人能帮我解决这个问题。

Critically inspecting your code, I believe you are using summernote in a wrong way. 严格检查您的代码,我相信您以错误的方式使用了summernote。 Here is a fix for your code 这是您的代码的修复

$("#btnAddVariable").click(function (e) {
    var eVariable = $("#EmailVariable option:selected").val();

    var txt = $(".summernote").('code'); //get the summernote text
        var caretPos = $txt[0].selectionStart;
        var textAreaTxt = $txt.val();
        var txtToAdd = eVariable;
        $(".summernote").summernote('code', textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos));              
});

This is the correct way:这是正确的方法:

$("#btnAddVariable").click(function (e) {
// if the text you want to add is the value of the #EmailVariable dropdown
   var eVariable = $("#EmailVariable option:selected").val();

// if the text you want to add is the value of the #EmailVariable dropdown
// var eVariable = $("#EmailVariable option:selected").text();

    $(".summernote").summernote('editor.insertText',eVariable);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM