0

正如问题中所说,我试图从下拉菜单中获取一个元素并将其与存储的变量相关联,然后使用 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>
4

2 回答 2

0

实际上,我找到了一种仅使用 javascript 的方法,它可以与除日历日期之外的其他数字集相关:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
   <script language="javascript">
        function displayDaysInTheMonth()
        {
        var numberOfDays = document.monthForm.months.value;
    var element = document.getElementById('a')
    element.innerHTML= "has " + numberOfDays + " days";
        }
  </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 value = '31'>January</option>
                <option value = '28'>February</option>
                <option value = '31'>March</option>
                <option value= '30'>April</option>
                <option value = '31'>May</option>
                <option value = '30'>June</option>
                <option value = '31'>July</option>
                <option value = '31'>August</option>
                <option value = '30'>September</option>
                <option value = '31'>October</option>
                <option value = '30' >November</option>
                <option value = '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>
于 2013-01-25T13:31:15.090 回答
0

您可以使用jquery API来执行此操作:

要获取下拉值并将 Div 值设置为选定值,请执行此操作

function displayDaysInTheMonth()
{
       var days = $('#months option:selected').val(); //gets dropdown value
       $("#a").html("has " + days + " days"); //sets div value here
}

将您的 div 更改为

<div name='result' class='result' id = 'a'>has # days.</div>

将下拉更改为

<select name='months' onchange='displayDaysInTheMonth();' 
  style='float: left; margin: 0 10px 10px 15px;'>
    <option value='31'>January</option>
    <option value='28'>February</option>
    <option value='31'>March</option>
    <option value='30'>April</option>
    <option value='31'>May</option>
    <option value='30'>June</option>
    <option value='31'>July</option>
    <option value='31'>August</option>
    <option value='30'>September</option>
    <option value='31'>October</option>
    <option value='30' >November</option>
    <option value='31'>December </option>
</select>
于 2013-01-24T22:28:36.650 回答