正如问题中所说,我试图从下拉菜单中获取一个元素并将其与存储的变量相关联,然后使用 if 语句将变量放在网页上的另一个位置。我在下面的示例有一个包含月份的下拉列表,我想使用“getElementByID”发布天数。什么是使这项工作的好方法?ARRGH 我是一个完全的新手......任何帮助将不胜感激。这是我的 html/javascript,不包括 CSS 好坏-
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script language="javascript">
function displayDaysInTheMonth()
{
var thirty = document.monthForm.30
var thirtyone = document.monthForm.31
var twentyeight = document.monthForm.28
if (thirty == 'true' )
{
document.monthForm.getElementById('a').innerHTML = "has 30 days";
return;
}
else if (thirtyone == 'true' )
{
document.monthForm.getElementById('b').innerHTML = "has 31 days";
return;
}
else if (twentyeight == 'true' )
{
document.monthForm.getElementById('c').innerHTML = "has 28 days";
return;
}
}
</script>
</head>
<body>
<div class='container'>
<h2> David Platt's</h2>
<h2>Monthly Day Count</h2>
<form name='monthForm'>
<select name='months' onchange='displayDaysInTheMonth();' style='float: left; margin: 0 10px 10px 15px;'>
<option name = '31'>January</option>
<option name = '28'>February</option>
<option name = '31'>March</option>
<option name = '30'>April</option>
<option name = '31'>May</option>
<option name = '30'>June</option>
<option name = '31'>July</option>
<option name = '31'>August</option>
<option name = '30'>September</option>
<option name = '31'>October</option>
<option name = '30' >November</option>
<option name = '31'>December </option>
</select>
<div name='result' class='result' id = 'a' id = 'b' id = 'c' >has # days.</div>
<div class='clear'></div>
</form>
</div>
</body>
</html>