Select标签与input标签结合使用

先来视图:
在这里插入图片描述
直接上代码:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">

<script src='http://libs.baidu.com/jquery/1.10.2/jquery.min.js'></script>
<body>
<span style="width: 80px;"></span>
<div style=" width: 200px; padding:5px ;position:relative;height: 50px;" >
    <span style="position:absolute;top:5px;overflow:hidden;width:95%;height:32px;">
        <!--这个是个多选框,用onchange事件的时候 ,将这个select的id传进去-->
               <select name="year" id="select_year" style="width: 100%;height:30px;outline:0;" onchange="qlcTrainYear(this)">
                   <option value="请选择">请选择</option>
                      <option value="未知">未知</option>
                   <option value="请输入年">请输入年</option>
               </select>

    </span>
    <!---->
    <span style="position:absolute;top:7px;left:10px;margin-top:1px;margin-left:1px;width:120px;height:28px;border-radius:5px;">
        <!--这里是input框,定位到select的上面-->
              <input type="text" id="input_year"  placeholder="请选择" style="width:80%;height:24px;border:0pt;border-radius:5px;outline:0">
    </span>
</div>
<span style="width: 80px;"></span>
<div style=" width: 200px; padding:5px ;position:relative;height: 50px;" >
    <span style="position:absolute;top:5px;overflow:hidden;width:95%;height:32px;">
        <!--这个是个多选框,用onchange事件的时候 ,将这个select的id传进去-->
               <select name="month"  id="select_month" style="width: 100%;height:30px;outline:0;" onchange="qlcTrainMonth(this)">
                 <option value="请选择">请选择</option>
                  <option value="未知">未知</option>
                   <option value="请输入月">请输入月</option>
               </select>

    </span>
    <!---->
    <span style="position:absolute;top:7px;left:10px;margin-top:1px;margin-left:1px;width:80%;height:28px;border-radius:5px;">
        <!--这里是input框,定位到select的上面-->
              <input type="text" id="input_month"  placeholder="请选择" style="width:80%;height:24px;border:0pt;border-radius:5px;outline:0">
    </span>
</div>
<span style="width: 80px;"></span>
<div style=" width: 200px; padding:5px ;position:relative;height: 50px;" >
    <span style="position:absolute;top:5px;overflow:hidden;width:95%;height:32px;">
        <!--这个是个多选框,用onchange事件的时候 ,将这个select的id传进去-->
               <select name="day" id="select_day" style="width: 100%;height:30px;outline:0;" onchange="qlcTrainDay(this)">
          <option value="请选择">请选择</option>
                 <option value="未知">未知</option>
                   <option value="请输入日">请输入日</option>
               </select>

    </span>
    <!---->
    <span style="position:absolute;top:7px;left:10px;margin-top:1px;margin-left:1px;width:80%;height:28px;border-radius:5px;">
        <!--这里是input框,定位到select的上面-->
              <input type="text" id="input_day"  placeholder="请选择" style="width:80%;height:24px;border:0pt;border-radius:5px;outline:0">
    </span>
</div>
<script>
    var select_year = document.getElementById("select_year");
    var select_month = document.getElementById("select_month");
    var select_day = document.getElementById("select_day");

    <!--select的值赋给input框-->
    function qlcTrainYear(obj) {

        if(obj.value == "请输入年"){
            document.getElementById("input_year").value = "";
            document.getElementById("input_year").readOnly = false;
            document.getElementById("input_year").setAttribute( "placeholder","请输入年");
            select_month.options.length = 0;
            select_day.options.length = 0;
            select_day.options.add(new Option("请选择", "请选择"));
            select_month.options.add(new Option("请选择", "请选择"));
            select_month.options.add(new Option("未知", "未知"));
            select_month.options.add(new Option("请输入月", "请输入月"));
        }else if(obj.value == "请选择"){
            select_month.options.length = 0;
            select_day.options.length = 0;
            document.getElementById("input_year").readOnly = true;
            document.getElementById("input_year").value = "请选择";
            document.getElementById("input_month").value = "请选择";
            document.getElementById("input_day").value = "请选择";
            document.getElementById("input_month").readOnly = true;
            document.getElementById("input_day").readOnly = true;
            select_month.options.add(new Option("请选择", "请选择"));
            select_day.options.add(new Option("请选择", "请选择"));
        }else {
            //未知
            select_month.options.length = 0;
            select_day.options.length = 0;
            select_day.options.add(new Option("请选择", "请选择"));
            select_month.options.add(new Option("请选择", "请选择"));
            select_month.options.add(new Option("未知", "未知"));
            select_month.options.add(new Option("请输入月", "请输入月"));
            document.getElementById("input_year").value= obj.value;
            document.getElementById("input_year").readOnly = true;
        }
    }
    function qlcTrainMonth(obj) {
        if(obj.value == "请输入月"){
            document.getElementById("input_month").value = "";
            document.getElementById("input_month").readOnly = false;
            document.getElementById("input_month").setAttribute( "placeholder","请输入月");
            select_day.options.length = 0;
            select_day.options.add(new Option("请选择", "请选择"));
            select_day.options.add(new Option("未知", "未知"));
            select_day.options.add(new Option("请输入日", "请输入日"));
        }else if(obj.value == "请选择"){
            select_day.options.length = 0;
            document.getElementById("input_month").readOnly = true;
            document.getElementById("input_day").readOnly = true;
            document.getElementById("input_day").value = "请选择";
            document.getElementById("input_month").value = "请选择";
            select_day.options.add(new Option("请选择", "请选择"));
        }else {
            //未知
            select_day.options.length = 0;
            select_day.options.add(new Option("请选择", "请选择"));
            select_day.options.add(new Option("未知", "未知"));
            select_day.options.add(new Option("请输入日", "请输入日"));
            document.getElementById("input_month").value= obj.value;
            document.getElementById("input_month").readOnly = true;
        }
    }
    function qlcTrainDay(obj) {
        if(obj.value == "请输入日"){
            document.getElementById("input_day").value = "";
            document.getElementById("input_day").readOnly = false;
            document.getElementById("input_day").setAttribute( "placeholder","请输入日");
        }else if(obj.value == "请选择"){
            document.getElementById("input_day").readOnly = true;
            document.getElementById("input_day").value = "请选择";
        }else {
            //未知
            document.getElementById("input_day").value= obj.value;
            document.getElementById("input_day").readOnly = true;
        }
    }

</script>


</body>
</html>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值