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

时间:2019-06-03 14:45:54

标签: php sql

我正在使用一个统计应用程序,该程序会将玩家(从数据库中的数据中获得)附加到特定位置。我希望已经被选中的球员从即将到来的职位中消失,这样同一位球员就不能放在两个不同的位置。任何人都可以建议在这里做什么 这是我总结的代码

<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 个答案:

答案 0 :(得分:0)

使用您的播放器创建一个datalist,并在onchange()上使用input选择一个JQuery将其删除:

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

这将使用JavaScript / JQuery从列表中删除播放器。

如果要在添加播放器后更改播放器,则需要做更多的工作,首先将旧的播放器重新添加回datalist,然后再从中删除新播放器。那有道理吗?

更新

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