40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
|
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 = $('<div class="form-group"></div>')
|
||
|
$formGroup.append('<label>' + field.label + '</label>')
|
||
|
|
||
|
let $input
|
||
|
if (field.type === 'textarea') {
|
||
|
$input = $('<textarea class="form-control" name="' + field.name + '"></textarea>')
|
||
|
} else {
|
||
|
$input = $('<input type="' + field.type + '" class="form-control" name="' + field.name + '">')
|
||
|
}
|
||
|
|
||
|
$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')
|
||
|
})
|
||
|
}
|