下拉列表为什么固定为了第三个值且不能改变?
代码为一个简单的计算器,下拉列表在经过一些判断语句后就固定为了第三个值无法改变,通过console可以看出判断语句前是正常的,可是判断语句之后就固定为了第三个值。
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value=""></option>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' id="btn" onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
<script type="text/javascript">
function count(){
var txt1=parseInt(document.getElementById("txt1").value);
var txt2=parseInt(document.getElementById("txt2").value);
var fruit=document.getElementById("fruit");
var myselect=document.getElementById("select");
var select_val=myselect.selectedIndex;
console.log(select_val);
if (select_val=0){
fruit.value=txt1+txt2;
}
else if (select_val=1) {
fruit.value=txt1-txt2;
}
else if (select_val=2) {
fruit.value=txt1*txt2;
}
else if (select_val=3) {
fruit.value=txt1/txt2;
}
console.log(select_val);
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这块
搞清楚
=
和==
和===
的区别把
=
换成==
就行了