从对象文字获取链接值,onchange — Javascript/HTML选择 [英] Get link value from object literal, onchange--Javascript/HTML select

查看:103
本文介绍了从对象文字获取链接值,onchange — Javascript/HTML选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用开关案例例程来操作此菜单,但是我想将开关案例更改为对象文字.

I know how to operate this menu with a switch case routine, but I want to change my switch case to an object literal.

A部分知道如何获取onchange值并打开一个窗口. B部分知道如何在对象的name:value对中查找值,但前提是给它提供了与值匹配的硬编码名称.我必须结合A部分和B部分的功能.由于A部分和B部分都彼此独立地工作,因此将它们组合起来应该很简单,但是我已经花了四天的时间了,我也不知道该怎么做.

Part A knows how to get the onchange value and open a window. Part B knows how to find the value in a name:value pair in an object, but only if it's given a hard-coded name to match a value to. I have to combine Part A and Part B's capabilities. Since Part A and Part B both work independently of each other, it should be a simple matter to combine them, but I've been at it four days and I don't know how.

部分A:此选择菜单的值属性中的硬编码链接已替换为用作对象名称的值.照原样,这将按应有的方式打开选项卡,但我想从B部分中称为"lynk"的对象中获取链接的正确对应值.

PART A: This select menu had the hard-coded links in the value attributes replaced with values which are used as names in an object. As is, this opens tabs as it should, but I want to get the right corresponding value of the link from the object called "lynk" in Part B.

<select id="sed">
    <option value="" selected>Select:</option>
    <option value="prev" >PREV</option>
    <option value="next">NEXT</option>
    <option value="home">home</option>
    <option value="gimme">contribute</option>
</select>

<script>
document.getElementById('sed').addEventListener ('change', valuu);

function valuu() {
ddd = document.getElementById("sed").value;
window.open(ddd, '_self');
}
</script>

部分B:此脚本来自在线示例,该在线示例从对象中获取相应值的方法.如果粘贴到控制台中,它将以字符串形式返回正确的链接.正确的答案在脚本的最后一行中被硬编码为"gimme".我必须用从选择选项值中获取onchange值并在对象中查找相应链接的代码替换'gimme'.

PART B: This script is from an online example of a way to get the corresponding value from an object. It returns the correct link as a string if pasted into the console. The correct answer is hard-coded as 'gimme' in the last line of the script. I have to replace 'gimme' with code which gets the onchange value from the select option values and looks up the corresponding link in the object.

<script>
/*Works when pasted into console*/
function foo(bar) {
  var lynk = {
    'default': (''),
    'next': ('nextly.html'),
    'prev': ('previous.html'),
    'gimme': ('http://patreonx.com/bxaxly'),
    'home': ('index.html')
  };
  return lynk[bar];
}
foo('gimme');
</script>

推荐答案

这应该使其具有动态性.

This should make it dynamic.

function valuu() {
   ddd = document.getElementById("sed").value;
   window.open(foo(ddd), '_self');
}

您可以在一个函数中完成所有这些操作,

You can do all this in one function like,

document.getElementById('sed').addEventListener ('change', function(e){
   var lynk = {
    'default': (''),
    'next': ('nextly.html'),
    'prev': ('previous.html'),
    'gimme': ('http://patreon.com/bearly'),
    'home': ('index.html')
   };
   window.open(lynk[e.target.value]);
});

这篇关于从对象文字获取链接值,onchange — Javascript/HTML选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆