如何解决JS添加按钮将文本添加到Textarea无效
这些textarea字段位于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函数添加注释的方式不同。单击按钮以添加解决方案注释时,在控制台中看不到任何错误。
我知道textarea的ID对于解析字段是正确的。
我唯一想知道的是,由于ID包含一个点,而活动注释的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","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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。