我需要创建一个按钮,单击该按钮会自动填充表单的注释部分

I need to create a button, which when clicked, auto fills the notes section of a form

我绝不是 javascript 方面的专家,因此我们将不胜感激。就像标题所暗示的那样,我想创建一个按钮,单击该按钮后,将使用以下信息自动填充表单的注释部分; "about employee discount."

为您的按钮添加点击处理程序并更新 label/textarea/div/span 的 value/content。

以下解决方案是jQuery解决方案,如果您不知道什么是jQuery,请检查this video

在下面的 运行 脚本之前,将 jquery 库从 cdn 加载到您的页面(将此脚本粘贴到 head 标记中)

<script type="type/script" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>

HTML 代码:

<input type="button" value="process" id="btn"/>
<span id="notes"></span>

JS代码:

$(function(){
  $('#btn').on('click',function(){
     $('#notes').text('about employee discount.'); //assuming #notes is a span tag.
  });
});

这是你的 HTML:

<input type="button" value="auto fill" onclick="autofill('about employee discount.')" />

<input type="text" id="output1" />
<p id="output2"></p>

这是你的 javascript:

var output1 = document.getElementById("output1");
var output2 = document.getElementById("output2");

var autofill = function(text) {
    output1.value = text;
    output2.innerHTML = text;
}

在像 pspan 这样的元素中你必须使用 innerHTML 获取或设置值,在某些元素中,如 input 使用必须使用 value.

https://jsbin.com/namuci/edit?html,js,output