簡體   English   中英

提交前修改鐵型JSON

[英]Modify iron-form JSON before submitting

我想知道是否可以在提交鐵表格之前編輯它的JSON文件? 例如,如果我想添加一個不是來自任何形式輸入的數組,或者我想在其中添加唯一鍵...

如果可能的話,我相信會在表單提交之前,但是文檔沒有提及“如何截取JSON”或類似內容。

謝謝!

您可以在iron-form-presubmit事件處理程序中修改iron-formrequest對象。 對於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> 

碼筆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM