聚合物铁形式,提交数组

时间:2016-04-01 21:26:12

标签: javascript polymer

我想提交一个数组。在表单中,用户不断将tags添加到javascript数组中,直到他们按下提交。我该如何提交这个数组?我应该将其编码为JSON并修改iron-ajax元素吗?

<dom-module id="create-account">
<template>
    <paper-card>
        <form is="account_creation_form" id="form" method="post" action={{actionUrl}}>
            <iron-image src="http://lorempixel.com/100/100"></iron-image>
            <paper-input id="name" name="Name" on-blur="addName" label="First, Last"></paper-input>
            <paper-dropdown-menu label="School">
                <paper-listbox id="organization" class="dropdown-content">
                    <template is="dom-repeat" items={{organizations}}>
                        <paper-item on-click="addOrganization">{{item}}</paper-item>
                    </template>
                </paper-listbox>
            </paper-dropdown-menu>
            <paper-input id="subjectTags" name="SubjectTags" label="Tags" on-keydown="addTag"></paper-input>
            <iron-list items="[[rs_tags]]">
                <template>
                    <paper-item on-click="deleteTag">[[item]]</paper-item>
                </template>
            </iron-list>
            <paper-button raised on-click="submitForm">Submit</paper-button>
        </form>
    </paper-card>
</template>
<script>
    Polymer({
        is: 'create-account',
        properties: {
            actionUrl: String,
            organizations: Array,
            rs_name: String,
            rs_tags: { //The array I want to submit
                type: Array,
                value: []
            },
            rs_organization: String
        },
        addName: function(e) {
            this.rs_name = this.$.name.value;
        },
        addOrganization: function(e) {
            this.rs_organization = this.organizations[this.$.organization.selected];
        },
        addTag: function(e) {
            if (e.keyCode === 13) {
                var input = this.$.subjectTags;
                var val = input.value;
                if (val === "")
                    return;
                this.push('rs_tags', val);
                input.value = "";
            }
        },
        deleteTag: function(e) {
            tag = e.model.item;
            var index = this.rs_tags.indexOf(tag);
            this.splice('rs_tags', index, 1);
        },
        submitForm: function(e) {
            var response = JSON.stringify({name: this.rs_name, tags: this.rs_tags, 
                organization: this.rs_organization});
            var form = document.getElementById('form');
            form.addEventListener('iron-form-presubmit', function() {
                this.request.method = 'put';
                this.request.params = response;
            });
            //form.submit();
        }
    });
</script>

enter image description here

0 个答案:

没有答案