function Modal(modalTitle, fields, onSubmit) { this.modalTitle = modalTitle this.fields = fields this.onSubmit = onSubmit } Modal.prototype.GenericModalForm = function () { let self = this // 设置表单标题 $('#genericFormModalLabel').text(self.modalTitle) // 生成表单内容 let $form = $('#genericForm') $form.empty() self.fields.forEach(function(field) { let $formGroup = $('
') $formGroup.append('') let $input if (field.type === 'textarea') { $input = $('') } else { $input = $('') } $formGroup.append($input) $form.append($formGroup) }) // 显示模态框 $('#genericFormModal').modal('show'); // 处理表单提交 $('#genericFormSubmit').off('click').on('click', function() { let formData = {} $form.serializeArray().forEach(function(item) { formData[item.name] = item.value }) self.onSubmit(formData) $('#genericFormModal').modal('hide') }) }