如何从同一页面传两个ID到数据库及sort ID异常问题求助
Hey there! No worries at all—we all start somewhere, so don't hesitate to ask even the smallest questions. Let's break down your two issues one by one:
1. Fixing the Incorrect sort Attribute Display for Your Sections
It sounds like your show/hide logic is working, but both sections (supposed to have sort=1 and sort=2) are ending up with sort=0, and only the seniority-based sort affects both. Here are the most likely culprits and fixes:
- Check your initial HTML setup: Make sure the two sections have their
sortattributes correctly set to 1 and 2 in the raw HTML. Sometimes accidental typos or initial JS overrides can reset these values on page load. - Audit your dropdown change logic: When the dropdown switches sections, are you accidentally targeting all sections instead of just the active one? For example, if your code uses a selector like
$('[sort]')to update attributes, it'll modify every element with asortattribute. Instead, narrow it down to the visible section:// Wrong: Updates all sections $('[sort]').attr('sort', '0'); // Right: Only updates the currently visible section $('[sort]:visible').attr('sort', '0'); - Check the seniority sort function: The button/logic that triggers "sort by seniority" is probably applying the change to all sections instead of the active one. Use the same
:visibleselector trick here to limit it to the section that's currently displayed.
If you can share a snippet of your relevant JS/HTML, I can give even more targeted advice, but these steps should cover the most common issues.
2. Passing Two IDs to the Database from the Same Page
There are a few straightforward ways to send two IDs to your backend/database, depending on how you're submitting the data:
- Hidden form fields: If you're using an HTML form, add two hidden inputs that hold your IDs. When the form submits, these values will be sent along with other form data:
<form action="/submit-data" method="POST"> <input type="hidden" name="first_id" value="your-first-id-value"> <input type="hidden" name="second_id" value="your-second-id-value"> <!-- Other form fields here --> <button type="submit">Submit</button> </form> - AJAX request data: If you're using AJAX (like with jQuery or vanilla JS), include both IDs in the request payload. For example:
// Using jQuery AJAX $.post('/submit-data', { first_id: 'value1', second_id: 'value2' }, function(response) { // Handle the backend response here }); - URL parameters: For GET requests, you can append both IDs to the URL. Just make sure to encode them properly if they contain special characters:
const url = `/submit-data?first_id=${encodeURIComponent(value1)}&second_id=${encodeURIComponent(value2)}`; // Use this URL for navigation or a GET request - Session storage (temporary): If you need to pass IDs between interactions on the same page (before submitting), store them in
sessionStoragetemporarily:// Store IDs sessionStorage.setItem('firstId', 'value1'); sessionStorage.setItem('secondId', 'value2'); // Retrieve when submitting const firstId = sessionStorage.getItem('firstId'); const secondId = sessionStorage.getItem('secondId');
Important: No matter which method you choose, always validate and sanitize the IDs on the backend to prevent security issues like SQL injection.
内容的提问来源于stack exchange,提问作者Adith Ashok

