gpt4 book ai didi

Javascript - 基于动态填充下拉菜单的转到 url

转载 作者:行者123 更新时间:2023-11-28 18:17:19 25 4
gpt4 key购买 nike

我创建了一个条件表单,单击提交后需要定向到特定的产品页面。这段代码可以做到这一点吗?我无法弄清楚如何将数组值链接到 URL 变量。

Here's the JSFiddle

a=new Array("V1-1: 1/4-4 900-4500#", "V1-1 Light-Weight Compact Solution", "V1-2: 1/2-36 150-600#","V1-3: 1/2-2, 150-600#","V1-4: 4-36 900-4500#");
b=new Array('NexTech® R Series Valves','NexTech® E Series Valves','TrunTech® Valves', 'PulseJet Valves');
c=new Array('Coking Isolation Valves','Coking Switch Valves');
d=new Array('Three-Way Valves','Four-Way Valves');
e=new Array('IsoTech®');
f=new Array('Xactrol® Mark I Valves', 'Xactrol® Mark II Valves', 'Xactrol® Mark III Valves');
g=new Array('PulseJet Valves','Ecopack®');
h=new Array('AbrasoCheck® Slurry Check Valves', 'AbrasoTech® Slurry Ball Valves');
i=new Array('Electronic Relief Valves');
j=new Array('ValvXpress® Automated Valve Packages');
k=new Array('Acid Injection Valves');
l=new Array('Double Block-and-Bleed Valves');
m=new Array('Turbine Bypass System');
n=new Array('Check Valves');
o=new Array('ValvXpress®','EcoPack®','ValvPerformance Testing®','Slurry Valves','Acid Injection Valves','Double Block-and-bleed Valves','Rhinoite® Hardfacing','Switch Valves','HVOF RiTech®','Cryogenic Valves');

populateSelect();

$(function() {

$('#cat').change(function(){
populateSelect();
});

});


function populateSelect(){
cat=$('#cat').val();
$('#item').html('');


if(cat=='a'){
a.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='b'){
b.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='c'){
c.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='d'){
d.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='e'){
e.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='f'){
f.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='g'){
g.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='h'){
h.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}


if(cat=='i'){
i.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='j'){
j.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='k'){
k.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='l'){
l.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='m'){
m.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='n'){
n.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}

if(cat=='o'){
o.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}





}

最佳答案

首先,您违反了DRY principle 。不要为每组产品设置单独的变量,而是将每个产品存储在某种字典结构(例如对象)中。

这将是我的第一次修订

var dict = {
a: ["V1-1: 1/4-4 900-4500#", "V1-1 Light-Weight Compact Solution", "V1-2: 1/2-36 150-600#","V1-3: 1/2-2, 150-600#","V1-4: 4-36 900-4500#"],
b: ['NexTech® R Series Valves','NexTech® E Series Valves','TrunTech® Valves', 'PulseJet Valves'],
c: ['Coking Isolation Valves','Coking Switch Valves'],
d: ['Three-Way Valves','Four-Way Valves'],
e: ['IsoTech®'],
f: ['Xactrol® Mark I Valves', 'Xactrol® Mark II Valves', 'Xactrol® Mark III Valves'],
g: ['PulseJet Valves','Ecopack®'],
h: ['AbrasoCheck® Slurry Check Valves', 'AbrasoTech® Slurry Ball Valves'],
i: ['Electronic Relief Valves'],
j: ['ValvXpress® Automated Valve Packages'],
k: ['Acid Injection Valves'],
l: ['Double Block-and-Bleed Valves'],
m: ['Turbine Bypass System'],
n: ['Check Valves'],
o: ['ValvXpress®','EcoPack®','ValvPerformance Testing®','Slurry Valves','Acid Injection Valves','Double Block-and-bleed Valves','Rhinoite® Hardfacing','Switch Valves','HVOF RiTech®','Cryogenic Valves']
};

$(function() {
// attach an 'change' event handler
// THEN trigger the event handler to call the function from the start
$('#cat').change(populateSelect).trigger('change');
});

function populateSelect() {
// this refers to the current element
// get the selected value
var cat = this.value;
// always a good idea to cache your element that you will be re-using (maybe move it outside the function too)
var items = $('#item');

items.html('');
// check if there are products associated with the selected value
if (dict.hasOwnProperty(cat)) {
// show products
dict[cat].forEach(function(product) {
items.append('<option>' + product + '</option>');
});
}
}

现在,就您的实际问题而言。我们可以修改数组,使其也包含 url。为了简单起见,您可以使用数组的数组,例如

a: [["V1-1: 1/4-4 900-4500#", "url"], ["V1-1 Light-Weight Compact Solution", "url"], ...]

或对象数组以提高可读性,例如

a: [{ name: "V1-1: 1/4-4 900-4500#", url: "url" }, { name: "V1-1 Light-Weight Compact Solution", url: "url"}, ...]

这是我使用对象数组的第二次修订。 (我缩短了字典只是为了显示说明)。

var dict = {
a: [
{
name: "V1-1: 1/4-4 900-4500#",
url: "http://www.bing.com"
},
{
name: "V1-1 Light-Weight Compact Solution",
url: "http://www.google.com"
},
{
name: "V1-2: 1/2-36 150-600#",
url: "http://www.yahoo.com"
},
],
b: [
{
name: 'NexTech® R Series Valves',
url: 'http://www.nike.com'
},
{
name: 'NexTech® E Series Valves',
url: 'http://www.walmart.com'
}
],
c: [{
name: 'Coking Isolation Valves',
url: 'http://www.adidas.com'
}],
};

$(function() {
// cache element so that you don't re-search the DOM multiple times
var $items = $('#item');

$('#cat').change(populateSelect).trigger('change');
$('#goto').click(redirectToURL);

// place the functions within the document.ready so that they have access to the cached elements
function populateSelect() {
$items.html('');
if (dict.hasOwnProperty(this.value)) {
dict[this.value].forEach(function(product) {
// you can store the URL in HTML5-data attribute to use it later
$items.append('<option data-url="' + product.url + '">' + product.name +'</option>');
});
}
}

function redirectToURL() {
// get the URL from the selected option's data-url and redirect to it
window.location.href = $items.find(':selected').data('url');
}
});

从技术上讲,您不是在“提交”表单,而只是“重定向”——因此我不会使用提交按钮,而只是使用普通按钮。

<input type="button" id="goto" value="submit">

下面是 demo最终修订版。你必须修复字典。

关于Javascript - 基于动态填充下拉菜单的转到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40592092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com