JS 添加按钮以将文本添加到 Textarea 不起作用

JS to add button to add text to a Textarea does not work

这些文本区域字段位于 servicenow 页面上:

<textarea id="activity-stream-comments-textarea"></textarea>
<textarea id="incident.close_notes"></textarea>

我有这个 JS 可以向它们添加文本:

// text values for update and resolve strings
var text_update = "Last Action\n~~~~~~~~~~~~~~~~~~~~~~\nOn " + today + " I \n\nNext Action\n~~~~~~~~~~~~~~~~~~~~~~\nWait for reply\n\nNext Action Date\n~~~~~~~~~~~~~~~~~~~~~~\nNot known - review in 7 days if no update before then from customer.";

var text_resolve = "Symptoms\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nCause\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nSolution\n~~~~~~~~~~~~~~~~~~~~~~\n\n";

// create input element for the option to add an activity note
var inputNote=document.createElement("input");
inputNote.type="button";
inputNote.value="Add Note";
inputNote.onclick = AddNote;
inputNote.setAttribute("class", "btn btn-default btn-ref icon icon-info");
inputNote.setAttribute("style", "position:absolute; top:120px; right:40px; width:120px");
document.body.appendChild(inputNote);

// AddNote Function - add the note to the activity update textarea
function AddNote() {
    if (!$("#activity-stream-comments-textarea").val()) {
        $("#activity-stream-comments-textarea").val (text_update);
        $("#activity-stream-comments-textarea").css({'background-color':'yellow'});
        $("#activity-stream-comments-textarea").focus();
    }
}

// create input element for the option to add  resolution note
var inputResolve=document.createElement("input");
inputResolve.type="button";
inputResolve.value="Resolve";
inputResolve.onclick = AddResolve;
inputResolve.setAttribute("class", "btn btn-default btn-ref icon icon-info");
inputResolve.setAttribute("style", "position:absolute; top:160px; right:40px; width:120px");
document.body.appendChild(inputResolve);

// AddResolve Function - add the note to the activity update textarea
function AddResolve() {
    if (!$("#incident.close_notes").val()) {
        $("#incident.close_notes").val (text_resolve);
        $("#incident.close_notes").css({'background-color':'green'});
        $("#incident.close_notes").focus();
    }
}

添加注释的选项工作正常(添加到 id 值为 activity-stream-comments-textarea 的文本区域)。

但是,添加解决方案注释(到 ID incident.close_notes)的选项不起作用。

如,我点击按钮添加了一个解决方案注释,但注释并没有像用AddNote功能添加的那样添加。当我点击按钮添加解决方案注释时,我在控制台中没有看到任何错误。

我知道文本区域的 ID 对于分辨率字段是正确的。

我唯一想知道的是,添加解决方案注释的选项是否不起作用,因为 ID 包含一个点,而 activity 注释的 ID 不包含一个点?

我已经尝试单独使用解决方案选项进行测试,以隔离与 AddNote 函数的任何冲突,但没有任何区别。

你的假设是正确的,引起问题的点添加\将解决你的问题。

var text_resolve = "Symptoms\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nCause\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nSolution\n~~~~~~~~~~~~~~~~~~~~~~\n\n";

// create input element for the option to add  resolution note
var inputResolve=document.createElement("input");
inputResolve.type="button";
inputResolve.value="Resolve";
inputResolve.onclick = AddResolve;
inputResolve.setAttribute("class", "btn btn-default btn-ref icon icon-info");
inputResolve.setAttribute("style", "position:absolute; top:160px; right:40px; width:120px");
document.body.appendChild(inputResolve);

// AddResolve Function - add the note to the activity update textarea
function AddResolve() {
    if (!$("#incident\.close_notes").val()) {
        $("#incident\.close_notes").val (text_resolve);
        $("#incident\.close_notes").css({'background-color':'green'});
        $("#incident\.close_notes").focus();
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="incident.close_notes"></textarea>

另一种优雅的方式是使用:$('[id="incident.close_notes"]')

var text_resolve = "Symptoms\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nCause\n~~~~~~~~~~~~~~~~~~~~~~\n\n\nSolution\n~~~~~~~~~~~~~~~~~~~~~~\n\n";

// create input element for the option to add  resolution note
var inputResolve=document.createElement("input");
inputResolve.type="button";
inputResolve.value="Resolve";
inputResolve.onclick = AddResolve;
inputResolve.setAttribute("class", "btn btn-default btn-ref icon icon-info");
inputResolve.setAttribute("style", "position:absolute; top:160px; right:40px; width:120px");
document.body.appendChild(inputResolve);

// AddResolve Function - add the note to the activity update textarea
function AddResolve() {
         
    if (! $('[id="incident.close_notes"]').val()) {
         $('[id="incident.close_notes"]').val (text_resolve);
        $('[id="incident.close_notes"]').css({'background-color':'green'});
         $('[id="incident.close_notes"]').focus();
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="incident.close_notes"></textarea>

我不知道是不是打字错误,但你没有关闭 <textarea>