JavaScript-试图通过单击按钮向文本字段添加值

JavaScript-试图通过单击按钮向文本字段添加值,javascript,html,Javascript,Html,当用户单击时,无法使我的功能正常工作并将按钮的值添加到文本字段 HTML 和弦: JavaScript 功能添加字段(crd){ document.testing.field.value+=crd; } 真的很难理解这里到底出了什么问题 希望这显示了我正在努力实现的目标:您正在错误地访问对象 <script language="javascript" type="text/javascript"> function addToField(crd){ do

当用户单击时,无法使我的功能正常工作并将按钮的值添加到文本字段

HTML
和弦:
JavaScript

功能添加字段(crd){
document.testing.field.value+=crd;
}
真的很难理解这里到底出了什么问题


希望这显示了我正在努力实现的目标:

您正在错误地访问对象

<script language="javascript" type="text/javascript">
    function addToField(crd){
        document.getElementByName("testing").value += crd;
    }
</script>

功能添加字段(crd){
document.getElementByName(“测试”).value+=crd;
}
或者,给元素一个ID并使用getElementByID(“testing”).value。
我通常会发现这样做效果更好…

如果你只是在摆弄它,它的工作原理是将script标记放在你的HTML之上:

<script>
    var addToField = function(c) {
        document.testing.field.value += c;
    };
</script>
<form name="testing" action="test.php" method="get">Chords:
    <textarea name="field"> </textarea>
    <input type="button" value="G" onClick="addToField('G');">
    <input type="button" value="C" onClick="addToField('C');">
    <input type="button" value="Am" onClick="addToField('Am');">
    <input type="button" value="F" onClick="addToField('F');">
</form>

var addToField=函数(c){
document.testing.field.value+=c;
};
和弦:

您的JSFIDLE是错误的。你的问题是这里更好。您希望使用
document.getElementById('field')。value+=crd而不是
document.testing.field.value+=crd

试试这个:

<form name="testing" action="test.php" method="get">Chords:
    <textarea id="field" name="field"> </textarea> ---note the addition of the ID parameter
    <input type="button" value="G" onClick="AddToField('G');">
    <input type="button" value="C" onClick="AddToField('C');">
    <input type="button" value="Am" onClick="AddToField('Am');">
    <input type="button" value="F" onClick="AddToField('F');">
</form>

<script>
function AddToField(c) {
    document.getElementById('field').value += c;
};
</script>
和弦:
---注意添加了ID参数
函数AddToField(c){
document.getElementById('field')。值+=c;
};

此外,函数不应为camelCase,而应为Pascal case;)

谢谢雨果,帮了大忙!
<script>
    var addToField = function(c) {
        document.testing.field.value += c;
    };
</script>
<form name="testing" action="test.php" method="get">Chords:
    <textarea name="field"> </textarea>
    <input type="button" value="G" onClick="addToField('G');">
    <input type="button" value="C" onClick="addToField('C');">
    <input type="button" value="Am" onClick="addToField('Am');">
    <input type="button" value="F" onClick="addToField('F');">
</form>
<form name="testing" action="test.php" method="get">Chords:
    <textarea id="field" name="field"> </textarea> ---note the addition of the ID parameter
    <input type="button" value="G" onClick="AddToField('G');">
    <input type="button" value="C" onClick="AddToField('C');">
    <input type="button" value="Am" onClick="AddToField('Am');">
    <input type="button" value="F" onClick="AddToField('F');">
</form>

<script>
function AddToField(c) {
    document.getElementById('field').value += c;
};
</script>