Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在下拉框中选择项目时,如何在文本框中获取属性值?_Javascript_Html - Fatal编程技术网

Javascript 在下拉框中选择项目时,如何在文本框中获取属性值?

Javascript 在下拉框中选择项目时,如何在文本框中获取属性值?,javascript,html,Javascript,Html,注意:这个问题可能看起来重复,但我还没有找到我要找的 我试图从所选下拉列表项中获取属性值,并在下拉列表更改/更新时将其写入文本框 HTML代码: <select id="dropdown" onchange="ChooseContact()"> <option value='1' cal='One'>1</option> <option value='2' cal='Two'>2</option></select&

注意:这个问题可能看起来重复,但我还没有找到我要找的

我试图从所选下拉列表项中获取属性值,并在下拉列表更改/更新时将其写入文本框

HTML代码:

<select id="dropdown" onchange="ChooseContact()">
    <option value='1' cal='One'>1</option>
    <option value='2' cal='Two'>2</option></select>

<input id="abc" type="text">

选中live here

如果查看浏览器开发工具控制台,您将看到错误
选择未定义

然后查看函数,可以看到您正在使用它,但它从未被声明过

试一试

function ChooseContact() {

    var y=document.getElementById("dropdown").options[select.selectedIndex].getAttribute("cal");
    document.getElementById("abc").value = y;
}
function ChooseContact() {
    var select = document.getElementById("dropdown"); // declare "select" 
    var y = select.options[select.selectedIndex].getAttribute("cal");
    document.getElementById("abc").value = y;
}