JavaScript:根据下拉菜单更改链接值

JavaScript: Change link value according to dropdown menu

本文关键字:链接 下拉菜单 JavaScript      更新时间:2023-09-26

我有一个键值列表,其中键是一个人类可读的名称,该值应该适合链接,例如:

[ 
 {"name": "The home page",        "value": "/index.php"}, 
 {"name": "The Pictures Section", "value": "/pics/index.html"},
 ...
]

以及链接的关键值列表,例如:

[ 
 {"name" : "My Site",  "value:":"homesite.com/my_site"},
 {"name" : "His Site", "value:":"homesite.com/his_site"},
 {"name" : "Her Site", "value:":"homesite.com/her_site"}, 
 ...
]

我想要一个带有下拉菜单和链接列表的网页。用户将选择一个名称,并相应地附加链接列表。例如,如果用户选择了The Pictures Section,则会出现以下列表:

<a href="homesite.com/my_site/pics/index.html">  My Site   </a>  <br>
<a href="homesite.com/his_site/pics/index.html"> His Site  </a>  <br>
<a href="homesite.com/her_site/pics/index.html"> Her Site  </a>  <br>

我该怎么做?

下面的代码没有经过测试,我写它只是为了给您一个工作的起点。我希望这就是你想要的:

var sections = [ 
        {"name": "The home page",        "value": "/index.php"}, 
        {"name": "The Pictures Section", "value": "/pics/index.html"},
        ...
    ],
    urls = [ 
        {"name" : "My Site",  "value:":"homesite.com/my_site"},
        {"name" : "His Site", "value:":"homesite.com/his_site"},
        {"name" : "Her Site", "value:":"homesite.com/her_site"}, 
        ...
    ];
function generateSelectHtml() {
    var html = '<select id="sections" onchange="showSescion(this);">';
    for (var i = 0; i < sections.length; i++) {
        html += '<option value="' + sections[i].value + '">' + sections[i].name + '</option>';
    }
    html += "</select>";
    return html;
}
function generateSelectDom() {
    var el = document.createElement("SELECT");
    el.id = "sections";
    el.onchange = function() { showSection(el); };
    for (var i = 0; i < sections.length; i++) {
        var option = document.createElement("option");
        option.value = sections[i].value;
        option.innerText = sections[i].name;
        el.appendChild(option)
    }
    return el;
}
function showSection(box) {
    var container = document.getElementById("listContainer");
    for (var i = 0; i < urls.length; i++) {
        var item = document.getElementById("a");
        item.href = urls[i].value + box.value;
        item.innerText = urls[i].name;
        container.appendChild(item);
    }
}

当然,你还有很多需要改变的地方。我所做的是:generateSelectHtml方法为选择框返回html,您可以将其添加到dom中,如下所示:

document.getElementById("AN_ID").innerHTML = generateSelectHtml();

generateSelectDom方法创建实际的dom元素,因此您可以使用appendChild将其添加到现有的dom中。

showSection方法只是将选定的值输出到id为listContainer的元素中。

var arraya = [ 
 {"name": "The home page",        "value": "/index.php"}, 
 {"name": "The Pictures Section", "value": "/pics/index.html"},
 ...
]
var arrayb = [ 
 {"name" : "My Site",  "value:":"homesite.com/my_site"},
 {"name" : "His Site", "value:":"homesite.com/his_site"},
 {"name" : "Her Site", "value:":"homesite.com/her_site"}, 
 ...
]
//bind a click event to your a tags
$('a').click(function() {
//get the url of the tag
var url = $(this).attr('href');
loop through arrayb until you match the one you want
var site;
for (site in arrayb)
{
if (site.value == url)
{
break;
}
}
});
//loop through arraya matching on the name of the value matched above
var value;
for(value in arraya)
{
if (site.name == value.name)
{
break;
}
}
//this is your value
site.value;
});