doufu5401 2014-02-26 07:57
浏览 29
已采纳

更改选择取决于输入值

In my form i have three input and one select box ,In Select there are only three value John , Matt and Lucy, but the select box is all automatic its totally depend on Input which place above it and input have all datepicker .

when 1st input have value then Select box value should be John after this when 2nd input filled out the value will be Matt and last 3 input also get value than select should be Lucy My question is how can i populate date from database in a select when input have a value

            <form method="post" name="myform" class="compform" id="myform">
            <input type="hidden" name="UserName" value="<?php echo $_SESSION['UserName']; ?>" />
            <ul>
            <li>
              <p>Distribution Date:</p>
              <input type="text" name="DistributionDate" id="datepicker" value="" readonly />
            </li>
            <li>
              <p>Reback Date:</p>
              <input type="text" name="RebackDate" id="datepicker1" value="" readonly />
            </li>
            <li>
              <p>Final Date:</p>
              <input type="text" name="FinalDate" id="datepicker3" value="" readonly />
            </li>
            <li>
              <p>Worker:</p>
              <?php 
                            $sql = "SELECT workerid,name FROM crew where workerid<> 0 ORDER By PeopleID";
                            $result = mysql_query($sql);

                            echo "<select name='workerid' id='name'>";
                            while ($row = mysql_fetch_array($result)) 
                            {
            ?>
              <option <?php if($formId != '' && $res['workerid'] == $row['workerid']){  echo " selected='select' "; } ?> value='<?php echo $row['workerid']; ?>'><?php echo $row['name']; ?> </option>
              <?php         } 
                            echo "</select>";
                            ?>
            </li>
  • 写回答

1条回答 默认 最新

  • duankang5285 2014-02-26 08:25
    关注

    You have to use Ajax and use change function for each textbox like this

    Sample HTML

    <input type="text" id="txt1"/>
    <input type="text" id="txt2"/>
    <input type="text" id="txt3"/>
    <br>
    
    <select id="select">
     <option>Lucy</option>
      <option>John</option>
      <option>Matt</option>
    </select>
    

    Jquery

     $("#txt1").change(function(e){
        if($("#txt2").val() == "" && $("#txt3").val() == "")
        {
            $("#select").find('option:contains("John")').attr('selected','selected'); 
    
        }
    
    });
    
    $("#txt2").change(function(e){
        if($("#txt3").val() == "")
        {
           $("#select").find('option').removeAttr("selected");
            $("#select").find('option:contains("Matt")').attr('selected','selected');
        }
    
    });
    
    $("#txt3").change(function(e){
    
           $("#select").find('option').removeAttr("selected");
            $("#select").find('option:contains("Lucy")').attr('selected','selected');
    
    
    });
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用