在客户端节点上立即访问表单数据

时间:2015-12-23 17:31:59

标签: ajax node.js express mean

有没有办法在客户端立即访问提交的表单数据而无需将其从服务器传回?这是我的过程:

1) Form data is sent over to server
2) Form data is pulled down in params
3) Form data is stored as JSON in a route
4) Form data is accessed by an Ajax call on the client side

必须有更好的方法来做到这一点......对吗?

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。您可以使用jQuery,AngularJS或DOM。您甚至可以使用Express返回请求中发送的数据,然后返回响应:res.json(req.body)

在客户端上使用普通的旧JavaScript / DOM,对要访问的每个输入元素执行以下操作:

// 1. Give the element an id.
<input id="myElementId">

// 2. Reference the input element by its id.
var element = document.getElementById('myElementId');

// 3. Use the reference to get or set the input element's value.
console.log(element.value);
element.value = 'foo';