我想知道在提交之前是否可以编辑铁形式的JSON文件?例如,如果我想添加一个不会来自任何表单输入的数组,或者我想在其中添加唯一键...
如果有可能,我相信它会在预表单提交期间,但文档中没有说明"如何拦截JSON"或类似的东西。
谢谢!
答案 0 :(得分:2)
您可以在iron-form
事件处理程序中修改iron-form-presubmit
的{{3}}对象。对于POST
请求,表单数据存储在正文中,您可以通过this.$.form.request.body
访问该正文。对于其他请求类型,数据位于this.$.form.request.params
。此示例将一个数组添加到请求的正文中:
// template
<form is="iron-form" id="form" on-iron-form-presubmit="_presubmit" method="post">...</form>
// script
_presubmit: function(e) {
var body = this.$.form.request.body;
body['newkey'] = [1,2,3];
console.log('body', body);
},
<head>
<base href="https://polygit.org/polymer+1.11.3/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-form/iron-form.html">
<link rel="import" href="paper-input/paper-input.html">
<link rel="import" href="paper-button/paper-button.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<form is="iron-form" id="form" on-iron-form-presubmit="_presubmit" method="post" action="//httpbin.org/post">
<paper-input name="name" label="name"></paper-input>
<paper-button on-tap="_submit">Submit</paper-button>
</form>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-foo',
_presubmit: function(e) {
var body = this.$.form.request.body;
body['newkey'] = [1,2,3];
console.log('body', body);
},
_submit: function() {
this.$.form.submit();
}
});
});
</script>
</dom-module>
</body>