如何将单选按钮与输入文本字段绑定?

时间:2019-05-26 13:27:57

标签: javascript php html css

这是员工的注册表格。雇员是否临时工或临时工必须签入单选按钮。如果是临时工,他应该填写输入的合同编号,如果有聘用日期,则应填写。如何将单选按钮与输入绑定,以使他无法“交叉完成”,例如,如果他是临时工,则填写录用日期。我想让他仅在按下特定单选按钮时才填写每个输入。

php代码:

<?php include 'dbconfig.php';?>
<?php header('Content-Type: text/html; charset=utf-8');?>
<!DOCTYPE HTML PUCLIC "-//W3C//DTDHTML
4.0 Transitional//EN"><HTML>
  <HEAD>
      <link rel="stylesheet" type="text/css" href="logintest.css">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </HEAD>
  <button class="btn" TYPE="submit" name="goback" onclick="window.location.href='login.php'">Go Back </button>

      <?php
      error_reporting(E_ALL); ini_set('display_errors', 1);
      if(isset($_POST['submit']))
      {
        $sql = "INSERT INTO employee (empID,EFirst,ELast,username, passcode)
        VALUES ('".$_POST["empID"]."','".$_POST["EFirst"]."','".$_POST["ELast"]."','".$_POST["username"]."','".$_POST["passcode"]."')";

        $result = mysqli_query($conn,$sql);
        $answer=$_POST['kind'];
        if($answer='permament'){
          $sql1 = "INSERT INTO permament_employee (empID,Hiring_Date) VALUES ('".$_POST["empID"]."','".$_POST["date"]."')";
          $result1 = mysqli_query($conn,$sql1);
        }

        if ($answer='temporary'){
          $sql2= "INSERT INTO temporary_employee(empID) VALUES ('".$_POST["empID"]."')";
          $result2 = mysqli_query($conn,$sql2);
        }
        echo "<script> location.replace('login.php') </script>";
      }
      ?>

    <FORM METHOD="post" ACTION="">
        <div class="input-group">
        <label>id</label>
           <INPUT TYPE="text" name="empID" SIZE="30" required>
        </div>
        <div class="input-group">
           <label>First Name</label>
           <INPUT TYPE="text" name="EFirst" SIZE="30" required>
        </div>
        <div class="input-group">
           <label>Last Name</label>
           <INPUT TYPE="text" name="ELast" SIZE="30" required>
        </div>
        <div class="input-group">
            <label>username</label>
            <INPUT TYPE="text" name="username" SIZE="30" required>  
        </div> 
        <div class="input-group">
             <label>password</label>
             <INPUT TYPE="password" name="passcode" SIZE="30" required>
        </div>   
        <div class="input-group">
          <div class="some-class">
          <label> Permament  <input type="radio" name="kind" value="permament" id="permament" required>  <br>   <input type="date" name="date" value="date" id="date" required>  <br> </label>
          <label> Temporary  <input type="radio" name="kind" value="temporary" id="temporary" required>  <br>  <input type="number" name="ContractNr" value="ContractNr" id="ContractNr" placeholder="Contract Number" required>  <br>     </label>
          </div>
        </div>
        <br>
        <br>
        <br>
        <br>

        <button class="btn" TYPE="submit" name="submit">Submit Info </button>
        <button class="btn" TYPE="reset" name="Reset">Reset </button>
    </FORM>

</HTML>

2 个答案:

答案 0 :(得分:1)

您需要javascript。 您必须在单选按钮上绑定一个“更改”事件,并根据单选按钮的选定值设置输入的可见性。

答案 1 :(得分:1)

如果您使用的是jQuery,则可以添加以下内容:

<script>
    $(‘input[name=“kind”]’).change(function(){
        if($(this).val() == ‘temporary’){
            $(‘input[name=“date”]).hide();
            $(‘input[name=“ContractNr”]).show();
        }else{
            $(‘input[name=“date”]).show();
            $(‘input[name=“ContractNr”]).hide();
        }
    });
</script>

或者,如果您只想禁用:

<script>
    $(‘input[name=“kind”]’).change(function(){
        if($(this).val() == ‘temporary’){
            $(‘input[name=“date”]).prop(‘disabled’, true);
            $(‘input[name=“ContractNr”]).prop(‘disabled’, false);
        }else{
            $(‘input[name=“date”]).prop(‘disabled’, false);
            $(‘input[name=“ContractNr”]).prop(‘disabled’, true);
        }
    });
</script>