Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要使用增量并在文本框中显示的帮助吗_Javascript - Fatal编程技术网

Javascript 需要使用增量并在文本框中显示的帮助吗

Javascript 需要使用增量并在文本框中显示的帮助吗,javascript,Javascript,我们需要有一个文本框,在这个文本框中输入一个数字,点击一个按钮,它会以1的增量递增,同时保持在同一个文本框中。以下是我目前掌握的代码: <form action=#> <p> Current Count...<input type="text" id="txtCounter" value="0"> </p> <p> <input type="button" value="In

我们需要有一个文本框,在这个文本框中输入一个数字,点击一个按钮,它会以
1
的增量递增,同时保持在同一个文本框中。以下是我目前掌握的代码:

<form action=#>
    <p>
        Current Count...<input type="text" id="txtCounter" value="0">
    </p>
    <p>
        <input type="button" value="Increment Count" id="btnIncrement" onclick="btnIncrement_onclick()">
        <input type="reset">
    </p>
</form>
<noscript>This website requires JavaScript to be enabled.</noscript>

如果有人能向我解释怎么做,而不仅仅是给我答案。我不知道为什么我在这个问题上会遇到如此困难。

请尝试以下简单代码:

函数btnIncrement\u onclick()
{
//a将文本框指定为变量
var textbox=document.getElementById(“txtCounter”);
//获取textbox的值并添加1,然后更新textbox
textbox.value=parseInt(textbox.value)+1;
}


当前计数。。。

此网站要求启用JavaScript。
在HTML中:

在html中有一个
onclick=“btnIncrement\u onclick()”
,这意味着每次单击都会触发函数

在您的JS中:

function btnIncrement_onclick() {
    // Named as countTextbox you input. sou we can use it later.
    var countTextbox = document.getElementById("txtCounter");

    // Get the current value attribute of it, initialy 0.
    var txtCounterData = txtCounter.value;

    // The line above is not being used. but you can check it with a console.log like this:
    console.log(txtCounterData);

    // Now you are calling again your input and changing his value attribute. this ++  means a increment. so we are increasing +1;
    countTextbox.value++;
}
您应该阅读有关增量、运算符和DOM(按id选择标记并再次选择其属性的方式)的更多信息


抱歉,没有找到好的英文源代码。

Java
=/=
JavaScriptWhy此行
var txtCounterData=txtCounter.value
txtCounter.value
是问题所在-您在上一行中命名了变量
countTextBox
。请看一看。@adambuchanemissh他还没有足够的声誉来呕吐。
function btnIncrement_onclick() {
    // Named as countTextbox you input. sou we can use it later.
    var countTextbox = document.getElementById("txtCounter");

    // Get the current value attribute of it, initialy 0.
    var txtCounterData = txtCounter.value;

    // The line above is not being used. but you can check it with a console.log like this:
    console.log(txtCounterData);

    // Now you are calling again your input and changing his value attribute. this ++  means a increment. so we are increasing +1;
    countTextbox.value++;
}