Wählen Sie mehrere Optionen in der Dropdown -Liste aus und fügen Sie zu URL hinzu [Duplicate]JavaScript

Javascript-Forum
Anonymous
 Wählen Sie mehrere Optionen in der Dropdown -Liste aus und fügen Sie zu URL hinzu [Duplicate]

Post by Anonymous »

Ich möchte, dass ich Brand 1 und Brand 2 und Brand 3.
wählen kann, dass ich Multipe -Optionen aus einer Dropdown -Liste ohne Drücken von "Strg" auswählen kann? Nur durch Klicken? Wie ein Kontrollkästchen als Dropdown -Menü? Und dass die Optionswerte nur in der URL verbunden sind: Brand1,2,3 < /p>


">

Code: Select all


Choose Channel
Facebook ads
Instagram ads


Choose Brand
brand 1
brand 2
brand 3




// Initialize the filters array
var filters = [];

// Initialize filters with any pre-selected values on page load
document.addEventListener("DOMContentLoaded", function() {
// Get all select elements
var selects = document.querySelectorAll('select');

// Initialize the filters array with the correct size
filters = new Array(selects.length).fill("");

// Check for any pre-selected options
selects.forEach(function(select) {
if (select.selectedIndex > 0) { // If something is selected (not the disabled option)
filters[select.id] = select.value;
}
});

// Update URL on initial load
changeURL();
});

function changeURL() {
var yourUrl = "https://yourdomain.com"; // Base URL
var queryParams = [];

// Update the current changed value
var selects = document.querySelectorAll('select');
selects.forEach(function(select) {
filters[select.id] = select.value;
});

// Build query parameters, only including valid values
selects.forEach(function(select) {
var paramName = select.name;
var paramValue = filters[select.id];

// Only add to URL if there's a valid value
if (paramValue && paramValue !== "" && paramValue !== "undefined") {
queryParams.push(paramName + "=" + paramValue);
}
});

// Add the query string if we have parameters
if (queryParams.length > 0) {
yourUrl += "?" + queryParams.join("&");
}

// Display the result
document.getElementById("test").innerHTML = yourUrl;
}


Vielen Dank im Voraus für Ihre Hilfe!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post