我在访问(多)var user_id = document.getElementById('myselect').options[select.selectedIndex].user_id;
var user_email = document.getElementById('myselect').options[select.selectedIndex].user_email;
document.getElementById('display').innerHTML = 'User with ID' + user_id + " has email address " + user_email;
的信息时遇到问题
我发现this并试图改编它 - 但它不起作用。我错过了一些明显的东西(js新手)。希望在这里找到帮助!
<select id="myselect">
<option user_email="test@test.de" user_id=1>Test 0</option>
<option user_email="email@email.de" user_id=1>Test 1</option>
<option user_email="whyso@serious.de" user_id=1>Test 3</option>
</select>
<div id="display"></div>
@media screen and (max-width: 980px) {
....
.menu ul {
width: 100%;
position: absolute;
top: 120%;
left: 0px;
padding: 10px 18px;
box-shadow: 0px 1px 1px rgba(0,0,0,0.15);
border-radius: 3px;
background: #303030;
}
...
}
答案 0 :(得分:2)
试试这个:
var index = document.getElementById('myselect').selectedIndex;
var user_id = document.getElementById('myselect').options[index].getAttribute('user_id');
var user_email = document.getElementById('myselect').options[index].getAttribute('user_email');
document.getElementById('display').innerHTML = 'User with ID' + user_id + " has email address " + user_email;
答案 1 :(得分:1)
select.selectedIndex
。这应该有效:
var select = document.getElementById('myselect');
var user_id = select.options[select.selectedIndex].getAttribute('user_id');
var user_email = select.options[select.selectedIndex].getAttribute('user_email');
document.getElementById('display').innerHTML = 'User with ID' + user_id + " has email address " + user_email;