我應該使用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輸入的數字? –