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 求一下解题思路,完全不懂。
    • ¥30 关于#硬件工程#的问题:求一下解题思路
    • ¥15 运筹学对偶单纯行法构造扩充问题
    • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
    • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
    • ¥15 多ip服务器站群如何搭建l2tp服务器
    • ¥15 lvgl V9移植到linux开发板
    • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
    • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
    • ¥15 Sharepoint JS开发 付费技术指导