简体   繁体   English

更改选择取决于输入值

[英]change select Depending Upon input value

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 . 在我的表单中,我有三个输入和一个选择框,在“选择”中只有三个值John,Matt和Lucy,但是选择框是自动的,它完全取决于Input上方的Input和所有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 当第一个输入有值时,选择框值应为John;第二个输入填写时,值应为Matt;最后3个输入也应获得比select值高的值:露西我的问题是如何在选择时从数据库中填充日期?输入有一个值

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

You have to use Ajax and use change function for each textbox like this 您必须像这样对每个文本框使用Ajax并使用change函数

Sample HTML 范例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 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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM