【发布时间】:2017-09-27 05:28:46
【问题描述】:
我需要将输入表单中的值发送到 nodejs 服务器,该服务器会触发使用此值的计算,并需要在客户端使用计算结果更新 p 元素。
如何做到这一点?
这就是我所拥有的:
//服务器端:
app.post('/calculate/:id', function(req, res){
var title = 'Tax Calculation';
var tax= taxcalculation(req.params.id);
res.render('index', {
title: title,
tax: tax,
});
});
//客户端:
var income= document.getElementById("income");
var tax = document.getElementById("tax")
$(income).on('change', function() {
console.log("changed");
$.ajax({
type: 'POST',
url: '/calculate/'+income.value,
success: function() {
$('#tax').html('<%= tax %>');
},
error: function() { // if error occured
alert("Error occured, please try again");
},
});
});
【问题讨论】:
标签: javascript jquery html node.js express