weixin_33725807 2019-10-08 21:03 采纳率: 0%
浏览 27

选中时删除选项

I have a dynamically generated select tag. Basically, every time a table row gets generated, so does a select tag. This is all done with PHP and html:

<?php $a=0; ?>
<?php foreach($routes as $r){?>
   <tr>
      <td><?php echo $r['ROUTE']; ?></td>
      <td><?php echo $r['CARRIER']; ?></td>
      <td>
         <select id="driver_list<?php echo $a ?>" onChange="removeDriver()">
            <option value=''>Select</option>
      <?php foreach ($driver as $d){?>
            <option value="<?echo trim($d['DRIVER_NUMBER']);?>" 
      <?php echo ($driver == $d['DRIVER_NUMBER']) ? "selected" : ''?>> 
      <?php echo trim($d['DRIVER_NUMBER']) . ' - ' . $d['FIRST_NAME'] . ' ' . $d['LAST_NAME']?>
            </option>
      <?php } ?>
         </select>
      </td>
      <td class="right"><?php foreach ($route['action'] as $a) { ?>
          [ <a href="<?php echo $a['href']; ?>"><?php echo $a['text']; ?></a> ]
          <?php } ?>
      </td>
   </tr>
      <?php } ?>

Lets say I have 6 routes going out tomorrow and I have 12 drivers to choose from. I want to create a function that removes the value selected from the current list from the next dynamically generated list. Initially every list will have 12 drivers. When a driver gets 'assigned' a route, the remaining 5 lists will have 11 drivers to choose from, with the selected driver getting removed. So on, and so on. The idea here is that only 1 driver can be assigned a route. I have the same dynamically generated list for vehicles and would need to apply the same type of logic.

I added the following function(s) to disable/highlight the selected values in my lists. I also modified the php and html to:

var a = <?php echo $a; ?>;

function removeDriver(){    
    var x = document.activeElement.value;
    for(i=0; i<a; i++){     
        var elemname='driver_list'+ i;
        if (elemname !== document.activeElement.id) {
            if (x != ''){
                var y = document.getElementById(elemname).options;
                for(z=0; z<y.length; z++) {
                    if(y[z].value == x){                        
                        y[z].disabled=true;
                        y[z].style.color = 'red';
                        y[z].style.background = 'grey';
                    }
                }
            }
        }   
    }   
}

This works to interact with the other lists. However, I want to re enable the options when a user changes their mind.

  • 写回答

0条回答 默认 最新

      报告相同问题?

      悬赏问题

      • ¥30 计算机网络子网划分路由模拟操作
      • ¥15 MATLAB的画图问题
      • ¥15 c语言用fopen_s成功打开文件之后闪退
      • ¥20 用C++完成,并且运用数组
      • ¥30 求解电力系统潮流计算结果不收敛问题
      • ¥15 某易易盾点选data解析逆向
      • ¥15 系统崩溃,关于订单的处理
      • ¥15 datax-web连接hive为数据源时发生报错,如何解决?
      • ¥15 plink在进行gwas分析时总读取不到表型
      • ¥20 数据结构与c语言的实践内容