将所选目的地从下拉列表中删除到另一个下拉列表

时间:2017-06-03 14:57:49

标签: javascript php codeigniter

我目前正在使用PHP(Codeigniter)开发航班预订。目前我拥有的字段/单选按钮:

2单选按钮

1)单程

2)往返

2下拉列表

1)来自

2)

依旧......

当用户试图预订时,用户不能重复他输入的目的地"来自目的地"进入"到目的地"。示例:我有3个目的地; 1)国家1; 2)国家2; 3)国家3,然后用户尝试预订。从:国家1,到:国家1。 " To:"中的国家1不应该显示,因为他已经在From Destination中使用它。

注意:我首先要做的是,调用控制器中的所有可用目的地,然后使用我的foreach在我的视图中调用它。

<div class="pure-u-1-1 fromto">
               <div class="pure-u-1-1">
                  <label for="from" class="margin2px">From</label>
                  <!-- <input type="text" class="fromto"><br> -->
                  <select class="fromto" name="flight_from">
                  <?php foreach($flight as $a):?>
                    <option value ="<?= $a->flight_name?>" ><?= $a->flight_destination?></option>
                  <?php endforeach?>

                  </select>

               </div>
                <div class="pure-u-1-1">
                  <label for="to" class="tomargin">To</label>
                  <!-- <input type="text" class="fromto"><br> -->
                  <select class="fromto" name="flight_to">
                    <?php foreach($flight as $a):?>
                      <option value ="<?= $a->flight_name?>" ><?= $a->flight_destination?></option>
                    <?php endforeach?>
                  </select>
                </div>

问题:如何防止它重复我在目的地&#34;中选择的国家/地区&#34;进入&#34;到目的地&#34;?

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery:

&#13;
&#13;
$('select[name=flight_from]').on('change',function(){
  $("select[name=flight_to]").find('option').show();
  var from = $(this).find(":selected").val();
  $("select[name=flight_to]").val('');
  if ( from != "" ) {
      $("select[name=flight_to]").find( 'option:contains("'+from+'")' ).hide();
  }
})
&#13;
<select name="flight_from">
  <option value="">-- Please select depature --</option>
  <option>Country 1</option>
  <option>Country 2</option>
  <option>Country 3</option>
</select>

<select name="flight_to">
  <option value="">-- Please select destination --</option>
  <option>Country 1</option>
  <option>Country 2</option>
  <option>Country 3</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;