求助:自定义下拉选项自动填充并隐藏工单主题栏功能异常排查
问题排查与修复方案
核心问题分析
你当前代码的问题在于仅在页面加载时获取一次下拉框的值,此时用户还未选择任何选项,subject_value始终是空值,提交时自然无法填充有效内容;同时没有监听下拉框的选择变化,无法实时同步选中值。
修复方案一:实时监听下拉框变化
// Hide Subject and auto populate with custom field dropdown selection $(document).ready(function () { var ticketForm = location.search.split('ticket_form_id=')[1]; let subject_value = ''; if (ticketForm == 123456789) { $('#request_subject').parent('.request_subject').hide(); // 监听下拉框选择变化,实时更新主题值 $('.request_custom_fields_987654321').on('change', function() { subject_value = $(this).val(); }); // 提交表单时填充最新选中值 $("#new_request").submit(function(e) { $('#request_subject').val(subject_value); }); } })
修复方案二:提交时直接获取当前值(更简洁)
// Hide Subject and auto populate with custom field dropdown selection $(document).ready(function () { var ticketForm = location.search.split('ticket_form_id=')[1]; if (ticketForm == 123456789) { $('#request_subject').parent('.request_subject').hide(); // 提交时直接拉取下拉框当前选中值 $("#new_request").submit(function(e) { const selectedValue = $('.request_custom_fields_987654321').val(); $('#request_subject').val(selectedValue); }); } })
内容的提问来源于stack exchange,提问作者Noelle
相关产品推荐
相关产品推荐

