【发布时间】:2015-01-28 06:18:16
【问题描述】:
我想根据用户选择的Type of Event 添加更多字段。
在不刷新页面的情况下根据用户选择添加其他字段的最佳方法是什么?
当用户单击 Add the details 并返回 html 时,我在想 ajax 调用?
有没有办法使用带有一系列 if/else 条件的模板系统来做到这一点?
【问题讨论】:
我想根据用户选择的Type of Event 添加更多字段。
在不刷新页面的情况下根据用户选择添加其他字段的最佳方法是什么?
当用户单击 Add the details 并返回 html 时,我在想 ajax 调用?
有没有办法使用带有一系列 if/else 条件的模板系统来做到这一点?
【问题讨论】:
这里想到了两个解决方案...
1)
将 jQuery 更改处理程序附加到“事件类型”选择元素,并执行 ajax 请求以返回需要显示的动态字段。
$('#TYPE_OF_EVENT_ID').change(function() {
$.get('/api/to/return/dynamic/fields/', {'type_of_event': $(this).val()}, function(data, textStatus, jqXHR) {
# Update DOM with dynamic content return by data (should probably be JSON)
});
});
2)
将逻辑直接硬编码到您的 javascript 中以处理 case 语句,以根据在“事件类型”选择元素中选择的值显示动态字段。
$('#TYPE_OF_EVENT_ID').change(function() {
switch($(this).val()) {
case 'Special Event':
# Show Special Event Fields
case 'Non Special Event':
# Show Non Special Event Fields
}
});
我推荐选项 1,因为它可以更好地扩展,让服务器上的逻辑由数据库驱动。
【讨论】: