doubi4435 2019-06-03 14:45
浏览 47
已采纳

选择后,从后续选择列表中删除选项

I am using a stats app that will attach players (obtained from data within a database) to particular positions. I want players to disappear from being selected in forthcoming positions if they have already been selected so that the same player cannot be put in two separate positions. Can anyone suggest what to do here This is the code, which i have summarised

<div class="row">
      <div class="col-sm-4">
          <label>1. Goalkeeper</label>
      </div>
      <div class="col-sm-4">
        <select class="form-control">
          <option value="" disabled selected>Select player</option>
          <?php
          include('conn.php');

          $selectplayer= "SELECT * from panel";

          $playerresult=$conn->query($selectplayer);

          if(!$playerresult){
            echo $conn->error;
          }
          while ($select=$playerresult->fetch_assoc()){
            $identify=$select['id'];
            $name=$select['name'];
            echo "<option>$identify. $name</option>";
          }


           ?>


        </select>
      </div>

    </div>
    <div class="row">
      <div class="col-sm-4">
          <label>2. Right corner back</label>
      </div>
      <div class="col-sm-4">
        <select class="form-control">
          <option value="" disabled selected>Select player</option>
          <?php
          include('conn.php');

          $selectplayer= "SELECT * from panel";

          $playerresult=$conn->query($selectplayer);

          if(!$playerresult){
            echo $conn->error;
          }
          while ($select=$playerresult->fetch_assoc()){
            $identify=$select['id'];
            $name=$select['name'];
            echo "<option>$identify. $name</option>";
          }


           ?>

        </select>
      </div>

    </div>
    <div class="row">
      <div class="col-sm-4">
          <label>3. Full back</label>
      </div>
      <div class="col-sm-4">
        <select class="form-control">
          <option value="" disabled selected>Select player</option>
          <?php
          include('conn.php');

          $selectplayer= "SELECT * from panel";

          $playerresult=$conn->query($selectplayer);

          if(!$playerresult){
            echo $conn->error;
          }
          while ($select=$playerresult->fetch_assoc()){
            $identify=$select['id'];
            $name=$select['name'];
            echo "<option>$identify. $name</option>";
          }


           ?>

        </select>
      </div>

    </div>
  • 写回答

1条回答 默认 最新

  • douchong4730 2019-06-03 15:22
    关注

    Create a datalist with your players, and JQuery to remove from it when selected one using onchange() on the input:

    function resetPlayerList(pos) {
     //Get player name
     var player = $('input#' + pos).val();
     //If player exists in datalist
      if($('option[value="' + player + '"]')) {
        //Remove player from datalist meaning other inputs cannot add the player
       $('option[value="' + player + '"]').remove();
      } 
      
      //If changing a player, you need to add the old player back into the datalist. This code does not do that for you.
      
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <datalist id="players">
      <option value="1">
      <option value="2">
      <option value="3">
      <option value="4">
      <option value="5">
      <!-- etc. -->
    </datalist>
    
    <input id="CF" onchange="resetPlayerList('CF')" list="players"> <!-- Center Forward -->
    <br/><br/>
    <input id="LW" onchange="resetPlayerList('LW')" list="players"> <!-- Left Wing -->

    This will remove the player from the list using JavaScript/JQuery.

    If you want to change a player after adding them in, you will need to do more work, to first re-add the old set player back to the datalist, before removing the new player from it... does that make sense?

    UPDATE

    <datalist id="players">
      <?php
      $players = array(
          array(
             'name' => 'Player 1'
          ),
          array(
              'name' => 'Player 2'
          )
       );
    
       //$players should be your array/object of players from SQL.
    
       for($players as $player) {
          echo '<option value="' . $player['name'] . '" />'; //or $player->name if object
       }
    ?>
    </datalist>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容