onblur是否支持多个if else语句?

时间:2016-05-27 00:34:25

标签: javascript html if-statement

<html>
<body>
    <input type="text" value="0"    id="x1" onblur="ThisEvent()"/>x1</br>               
    <input type="text" value="0"    id="x2">x2      </br>

<script>
    var x1      = document.getElementById("x1");    
    var x2      = document.getElementById("x2");

function ThisEvent(){
    if (x1=1)   {x2.value--;}else{
    if (x1=2)   {x2.value++;}else{
    if (x1=3)   {x2.value+0;}}}
    }
</script>
</body>
</html>

似乎只触发第一个if语句并忽略其他情况。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

=是一个赋值运算符。

==&amp; ===是比较运算符。 (参见this question了解它们之间的区别)

它停在第一个if语句上,因为x1=1返回1,这是真的。

切换到使用比较运算符后,您应该使用x1.value == 1来比较<input>的数值,或者使用x1.value === "1"

答案 1 :(得分:0)

<html>
<body>
    <input type="text" value="0"id="x1" onblur="ThisEvent()">x1     </br>   
    <input type="text" value="0"    id="x2"     >   x2      </br>
<script>
    var x1      = document.getElementById("x1");    
    var x2      = document.getElementById("x2");

function ThisEvent(){// needs a lot of work done to it
    if (x1.value==1)    {x2.value--;}else{
    if (x1.value==2)    {x2.value++;}else{
    if (x1.value==3)    {x2.value+0;}}}}
</script>
</body>
</html>