用JS根据选择框的值来给输入框赋值

用JS根据下拉选择框有三个值通过投入方式控制字段值如果投入方式选择新买 则设备租赁总费用预算等于0和设备折旧总费用预算等于0如果投入方式等于新租 则设备购买总费用预算等于0和设备折旧总费用预算等于0如果投入方式等于折旧 则设备租赁总费用预算等于0和设备购买总费用预算等于0

自己再按自己要求改改吧。

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<select id="s">
<option x=1 y=2>新租</option>
<option x=3 y=4>新买</option>
<option x=5 y=6>折旧</option>
</select>
<input id="x" type="" name="">
<input id="y" type="" name="">
</body>
<script>
document.getElementById('s').onchange=function(){
x=this.options[this.selectedIndex].getAttribute('x');
y=this.options[this.selectedIndex].getAttribute('y');
document.getElementById('x').value=x;
document.getElementById('y').value=y;
}
</script>
</html>

追问

当页面加载时这个select框没有,后来点击页面按钮有了这个select ,这就导致一开始页面加载时ID为空这怎么办?

追答

温馨提示:答案为网友推荐,仅供参考