Javascript - 如何添加两个文本框的结果并在第三个显示结果?

时间:2017-11-04 15:19:08

标签: javascript jquery html netbeans

我有两个基于带onclick的按钮逐步添加/减去值,如下所示,我试图将这两个框的输出添加到第三个。

使用Javascript:

var i = 1;
function buttonClick() {
        i++;
    document.getElementById('inc').value = i;
    }
function buttonClickA() {
        i--;
    document.getElementById('inc').value = i;
    }

  var w = 1;
function buttonClickC() {
        w++;
    document.getElementById('inc1').value = w;
    }
function buttonClickD() {
        w--;
    document.getElementById('inc1').value = w;
    }
function sum() {
        var txtFirstNumberValue = document.getElementById('inc').value;
            var txtSecondNumberValue = document.getElementById('inc1').value;
                var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
                    if (!isNaN(result)) {
                        document.getElementById("tot").value = result;
        }
    }

HTML:

<button onclick="buttonClick()">Add</button>
<input type="text" id="inc" value="0">
<button onclick="buttonClickA()">Subtract</button>

<button onclick="buttonClickC()">Add</button>
<input type="text" id="inc1" value="0">
<button onclick="buttonClickD()">Subtract</button>

<input type="text" id="tot" />

它添加了第三个文本框,前两个工作正常,但第三个没有新输出,不确定我做错了什么。

1 个答案:

答案 0 :(得分:1)

您还没有调用public static boolean tryWriteBinarySTL(Triangle t, ByteBuffer buf) { final int triangleBytes = 50; // set this. if (buf.remaining() < triangleBytes) { return false; } buf.putFloat((float)t.normal.x); // ... return true; } // Create a new path to your file on the default file system. Path filePath = Paths.get("file.txt"); // Open a channel in write mode on your file. try (WritableByteChannel channel = Files.newByteChannel(filePath, StandardOpenOption.WRITE)) { // Allocate a new buffer. ByteBuffer buf = ByteBuffer.allocate(8192); // Write your triangle data to the buffer. for (Triangle triangle : triangles) { while (!tryWriteBinarySTL(triangle, buf) ) { // Flush buffer. buf.flip(); while (buf.hasRemaining()) { channel.write(buf); } buf.flip(); } } // Write remaining. buf.flip(); channel.write(buf); } 函数。 每按一次按钮调用sum()功能。