Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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,不确定我是否在以下javascript代码中获得了正确的对象: <form> What is 5 + 5?: <input type ="number" id ="num_answer;"/> </form> <script> function basic_math(){ var num_val = document.getElementById('num_answer').value; if (nu

不确定我是否在以下javascript代码中获得了正确的对象:

<form>
    What is 5 + 5?: <input type ="number" id ="num_answer;"/>
</form>
<script>
    function basic_math(){
        var num_val = document.getElementById('num_answer').value;
        if (num_val == 10) {
            document.getElementById('num_answer').style.backgroundColor = green;
        }
        else{
            document.getElementById('num_answer').style.backgroundColor = red;
        }
    }
</script>


<button type = "button" onclick ="basic_math();"> Solve! 
</button>

去掉分号:

什么是5+5 应该是:

<input type ="number" id="num_answer"/>

没有分号这里有一个使用JQuery的解决方案

<p> <span> What is 5 + 5? </span> 
  <input type="number" id="num_answer" />    </p>    
<p> <span> What is 5 + 5? </span> 
  <input type="number" id="num_answer" />    </p>    
function basic_math()
{   
   num_val = $("#num_answer").val();
   if (num_val == 10) {
    $('#num_answer').css("color", "white");
    $('#num_answer').css("background-color", "green");
   } else {

     $('#num_answer').css("background-color", "red");

     }
}