我应该使用parseInt添加用户输入的两个数字,然后使用我的响应对象将结果和显示输出发回给用户。如何使用parseInt添加两个数字?
举例来说,如果用户输入2和3,服务器应该把这些数字加起来并传回含有值5
对象这里是我到目前为止/试过有: 指数。 JS
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/add', function (request,response) {
console.log('add method called');
console.log('The parameters are: ', request.query);
//Attempt 1
//var parse = parseInt('#operatorA',12);
// var parse2= parseInt('operatorB',7);
//Attempt 2
//var operatorA = $('#operatorA').val();
// var operatorB = $('#operatorB').val();
//Attempt 3
//var a = parseInt(" ") ;
//var b = parseInt(" ") ;
//var x = parseInt (a + b);
//Attempt 4
//var x = a+b;
//Attempt 5
//var a = operatorA this won't work b/c operator a isn't defined here.
//Attempt 6
//var a = $('#operatorA').val();
//var b = $('#operatorB').val();
//var x = parseInt(a);
//var y = parseInt(b);
response.send([
//Attempt 1
//parse
//Attempt 2
//parseInt(operatorA,operatorB)
//Attempt 3 & 4
// x
//Attempt 6
//x + y
]);
});
module.exports = router;
Control.js
$(document).ready(function() {
'use strict';
console.log('Document loaded in isit320');
$('#read').click(read);
$('#readJson').click(callReadJson);
$('#add').click(add);
}
function add() {
var operatorA = $('#operatorA').val();
var operatorB = $('#operatorB').val();
console.log('operators: ', operatorA, operatorB);
var requestQuery = { operatorA: operatorA, operatorB: operatorB };
$.getJSON('/add', requestQuery,function(result) {
console.log(result);
$('#display').html(JSON.stringify(result));
});
}
});
不知道从何处取得这些想法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt – epascarello
如果您可以描述您遇到的问题并将其减少到重现该问题所需的最小代码量,这将会很有帮助。就目前而言,你其实并没有问过一个问题 – billynoah
嘿,比利,对不起,我对堆栈溢出还很陌生。问题是如何添加到用户使用parseInt输入的数字? –