Odoo 11 Qweb模板:依Many2one字段选择控制招聘页面字段显隐
Hey there! Let's get your Odoo recruitment page working just the way you want—showing hidden fields when a specific university is selected from that dropdown. Here's a straightforward approach that fits right with your existing code snippet:
Step 1: Mark Your Hidden Fields with a CSS Class
First, wrap the fields you want to hide by default in a container, and give it a class (like hidden-field) plus a unique ID so we can target it later. For example:
<!-- Your existing university dropdown --> <div class="col-md-3 col-sm-3"> <select class="form-control" id="university_id" name="university_id"> <t t-foreach="universities" t-as="university"> <option t-att-value="university.id" t-esc="university.name"/> </t> </select> </div> <!-- Hidden fields container --> <div class="col-md-3 col-sm-3 hidden-field" id="target-university-fields"> <!-- Add your hidden input fields here, e.g.: --> <input type="text" class="form-control" name="alumni_year" placeholder="Alumni Graduation Year"/> </div>
Add this CSS to hide the container initially:
.hidden-field { display: none; }
Step 2: Add JavaScript to Listen for Dropdown Changes
Odoo uses jQuery by default, so we can leverage that to detect when the user selects a university and toggle the hidden fields. Add this script either in your QWeb template or a custom JS file:
$(document).ready(function() { // Listen for changes on the university dropdown $('#university_id').on('change', function() { const selectedUniId = $(this).val(); // Replace this with the specific university ID(s) that should trigger the hidden fields const triggerUniIds = [3, 7]; // Example: IDs of universities that need extra fields // Check if the selected ID is in our trigger list if (triggerUniIds.includes(parseInt(selectedUniId))) { $('#target-university-fields').show(); } else { $('#target-university-fields').hide(); } }); // Optional: Check initial value when the page loads (in case of edit mode) const initialUniId = $('#university_id').val(); if (triggerUniIds.includes(parseInt(initialUniId))) { $('#target-university-fields').show(); } });
Key Notes to Keep in Mind
- Target Reliability: If Odoo dynamically modifies the dropdown's ID (e.g., adding a prefix), use the
nameattribute instead for selection:$('select[name="university_id"]') - Multiple Triggers: The example uses an array of IDs, so you can easily add more universities that should show the hidden fields
- Edit Mode: The optional initial check ensures the hidden fields are visible if the record already has a triggering university selected when the page loads
内容的提问来源于stack exchange,提问作者exanoid
相关产品推荐
相关产品推荐

