对下拉列表选项的操作

时间:2017-07-15 08:29:25

标签: javascript html

我无法在选择下拉选项时获得这两个值。只有一个输出不同时给出。请帮忙解决这个问题。我想在输出字段中显示值,当我选择fahrenheiet时它应该以摄氏度给出,反之亦然。

<script type="text/javascript">

			function findCelsius()
			{
				var vals = document.getElementById("convert").value;
				if(vals = "F"){
					var	v = document.getElementById("vlues").value;
					var	celsius = (v - 32) * 5/9;
					
					var result = Math.ceil(celsius);
					
					document.getElementById("answer").value = result;
					document.getElementById("output").innerHTML = "You Selected: Farenheit to Celsius!";
				}
				else if(vals = "C")
				{
					var va = document.getElementById("vlues").value;
					var fah = (va * 9/5) + 32;
					
					var result = Math.ceil(fah);
					
					document.getElementById("answer").value = result;
					document.getElementById("output").innerHTML = "You Selected: Celsius to Fahrenheit!";
				}
		
		}
		
		
</script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Conversion</title>
</head>

<body>
<h2>Convert Temprature</h2>
<form name="conversion">
	<table>
    	<tr>
        	<td>
            	Enter a Value
            </td>
            <td>
            	<input type="number" name="vlue" value="" id="vlues">
            	<select id="convert" onChange="findCelsius()">
                	<option value="F">Farenheight(F°)</option>
                    <option value="C">Celsius(C°)</option>
                </select>
            </td>
        </tr>
        <tr>
        	<td>
            </td>
            <td>
            	<input type="text" id="answer" name="result" value="">
            </td>
        </tr>
        <tr>
            <td>
            	Answer:
            </td>
		</tr>
        <tr>
        	<td>
            </td>
            <td>
            	<span id="output"></span>
            </td>
        </tr>
	</table>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果条件声明错误,请使用==代替=

&#13;
&#13;
function findCelsius() {
  var vals = document.getElementById("convert").value;
  if (vals == "F") {
    var v = document.getElementById("vlues").value;
    var celsius = (v - 32) * 5 / 9;

    var result = Math.ceil(celsius);

    document.getElementById("answer").value = result;
    document.getElementById("output").innerHTML = "You Selected: Farenheit to Celsius!";
  } else if (vals == "C") {
    var va = document.getElementById("vlues").value;
    var fah = (va * 9 / 5) + 32;

    var result = Math.ceil(fah);

    document.getElementById("answer").value = result;
    document.getElementById("output").innerHTML = "You Selected: Celsius to Fahrenheit!";
  }

}
&#13;
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Conversion</title>
</head>

<body>
  <h2>Convert Temprature</h2>
  <form name="conversion">
    <table>
      <tr>
        <td>
          Enter a Value
        </td>
        <td>
          <input type="number" name="vlue" value="" id="vlues">
          <select id="convert" onChange="findCelsius()">
                	<option value="">select</option>
                	<option value="F">Farenheight(F°)</option>
                    <option value="C">Celsius(C°)</option>
                </select>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td>
          <input type="text" id="answer" name="result" value="">
        </td>
      </tr>
      <tr>
        <td>
          Answer:
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td>
          <span id="output"></span>
        </td>
      </tr>
    </table>
  </form>
</body>

</html>
&#13;
&#13;
&#13;