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

JavaScript018

用JS根据选择框的值来给输入框赋值,第1张

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

代码如下:

<!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>

<option selected value="month">月</option>

<option value="year" selected>年</option>

这句话有问题,一般都是一个option被选定,没有两个的 改成

<option value="year" selected>年</option>

<option value="month">月</option>

把这个<div style= "display:none"></div>去掉 同时把改成

<div id="year_select" style= "display:block">

<div id="month_select" style= "display:none">

js 为:

function selects(myselect){

if(myselect=="year") {

document.getElementById("year_select").style.display = "block"

document.getElementById("month_select").style.display = "none"

} else if(myselect=="month"){

document.getElementById("year_select").style.display = "none"

document.getElementById("month_select").style.display = "block"

}

}

默认情况下是显示<div id="year_select">这个div的,如果不想显示,则改成

<option value="date" selected>请选择日期</option>

<option value="year">年</option>

<option value="month">月</option>

然后

<div id="year_select" style= "display:none">

<div id="month_select" style= "display:none">

js改为

function selects(myselect){

if(myselect=="year") {

document.getElementById("year_select").style.display = "block"

document.getElementById("month_select").style.display = "none"

} else if(myselect=="month"){

document.getElementById("year_select").style.display = "none"

document.getElementById("month_select").style.display = "block"

} else {

document.getElementById("year_select").style.display = "none"

document.getElementById("month_select").style.display = "none"

}

}