简体   繁体   English

如何在表中该行的其他可用下拉列表中显示从下拉列表中取消选择的选项-jQuery

[英]how to show the deselected option from the dropdown list in the other available dropdown list of that row in a table - jquery

I have a html table which has input text fields and dropdown list's as columns. 我有一个html表,其中具有输入文本字段和下拉列表的列。 I have two dropdown list columns which displays the same list of options. 我有两个下拉列表列,它们显示相同的选项列表。

I'm trying to remove the option selected in one dropdown and doesn't show in the other dropdown present in the same row which is working when i select a option from Select Product1 dropdown list. 我正在尝试删除在一个下拉列表中选择的选项,并且在我从“选择Product1”下拉列表中选择一个选项时,在同一行中存在的另一下拉列表中不显示该选项。

Issues I'm facing is when user first select the option from Select Product2 column dropdown list, it is not removing the option from Select Product1 column list for that row. 我面临的问题是,当用户首先从“ Select Product2列下拉列表中选择选项时,它没有从该行的“ Select Product1列列表中删除该选项。

Another issue is , if user selected one option in the dropdown list that option is removed in the other drop down list, if user changes the selected option from first dropdown list the removed element should be shown/available in the other dropdown list but it is not showing the first selected option as it has been already removed from the list. 另一个问题是 ,如果用户在下拉列表中选择了一个选项,则该选项已在另一个下拉列表中删除,如果用户从第一个下拉列表中更改了所选选项,则应在另一个下拉列表中显示/可用已删除的元素,但是未显示第一个选择的选项,因为它已从列表中删除。

Any inputs on this two issues would be helpful, i tried to find the solution but no luck. 在这两个问题上的任何投入都会有所帮助,我试图找到解决方案,但没有运气。

Sample Demo link 示例演示链接

Code: 码:

 //populate dropdown with the valu`enter code here`e function populateSelect() { var ids = [{"pid":"laptop","des":"laptop"},{"pid":"Mobile","des":"Mobile"},{"pid":"IPAD mini.","des":"IPAD mini."}, {"pid":"toys","des":"toys"},{"pid":"electronics","des":"electronics"},{"pid":"desktop","des":"desktop"},{"pid":"ipad Air","des":"ipad Air"}] var productDropdown1 = $(".product1"); var productDropdown2 = $(".product2"); $.each(ids, function(index,value) { $("<option />").text(value.des).val(value.pid).appendTo(productDropdown1); $("<option />").text(value.des).val(value.pid).appendTo(productDropdown2); }); $('select').change(function() { var $sel = $(this), val = $sel.val(); $sel.parent().siblings().find('.product2 option[value=' + val + ']').remove() }); } $(document).ready(function(){ populateSelect(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="orderTable" border="1"> <tr> <th>Or.ID</th> <th>Select Product1</th> <th>Description</th> <th>Select Product2</th> <th>Comments</th> </tr> <tr> <td><input type="text" name="orderNum" value="" id="orderNum1"></td> <td> <select class="product1" id="prod1"> <option value=""></option> </select> </td> <td> <input type="text" name="description" value=""> </td> <td> <select class="product2" id="product2"> <option value=""></option> </select> </td> <td> <input type="text" name="Comments" value=""> </td> </tr> <tr> <td><input type="text" name="orderNum" value="" id="orderNum2"></td> <td> <select class="product1" id="prod2"> <option value=""></option> </select> </td> <td> <input type="text" name="description" value=""> </td> <td> <select class="product2" > <option value=""></option> </select> </td> <td> <input type="text" name="Comments" value=""> </td> </tr> <tr> <td><input type="text" name="orderNum" value="" id="orderNum3"></td> <td> <select class="product1" id="prod3"> <option value=""></option> </select> </td> <td> <input type="text" name="description" value=""> </td> <td> <select class="product2" > <option value=""></option> </select> </td> <td> <input type="text" name="Comments" value=""> </td> </tr> <tr> <td><input type="text" name="orderNum" value="" id="orderNum4"></td> <td> <select class="product1" id="prod4"> <option value=""></option> </select> </td> <td> <input type="text" name="description" value=""> </td> <td> <select class="product2" > <option value=""></option> </select> </td> <td> <input type="text" name="Comments" value=""> </td> </tr> </table> 

Every option in dropdown must have unique ID. 下拉菜单中的每个选项都必须具有唯一的ID。 When you select option "house" from dropdown menu "menu1", then remove/hide same option(option with same ID) from "menu2". 当从下拉菜单“ menu1”中选择选项“ house”时,然后从“ menu2”中删除/隐藏相同选项(具有相同ID的选项)。 Another way to do it is that every option has onClick that will call some function. 另一种方法是,每个选项都有onClick来调用某些功能。 Example: 例:

<option value="" onclick="hideOption("menu2-option1")" id="menu1-option1">Option 1</option>

<!-- JS CODE -->
function hideOption(hideOptionID)
{
    document.getElementById(hideOptionID).style.display = "none"; 
}

It is better to disable an option than remove it from the ui. 禁用选项比从ui中删除选项更好。 try to change 尝试改变

$('select').change(function() {
        var $sel = $(this),
                val = $sel.val();
        $sel.parent().siblings().find('.product2 option[value=' + val + ']').remove()

});

To

    $('select').change(function() {    
            var $sel = $(this),
            val = $sel.val();
            //$('select').not($sel).find('option').removeAttr('disabled');
 $sel.parent().siblings().not($sel).find('option').removeAttr('disabled');
        $sel.parent().siblings().find('select option[value=' + val + ']').attr('disabled',true);
        });

DEMO HERE 此处演示

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除在一个下拉列表字段中选择的选项,并且不显示在该行不存在的其他下拉列表中 - remove the option selected in one dropdown list field and don't show in other dropdown list present in that row not working- jquery 通过点击数据表行从下拉列表中选择一个选项 - Select an option from a dropdown list by onClick on data-table row 通过选择其他下拉列表的选项,将列表填充到下拉列表中,但完整的列会在表格中获取该行的选项 - List is populated into a dropdown, by selecting an option of other dropdown, but the complete column gets that row's options in a table 将其他选项的值添加到下拉列表 - Add value from other option to a dropdown list 如何在下拉选项中显示数组列表 - How to show array list in dropdown option 在下拉列表中显示mysql表中的数据 - Show data from mysql table in dropdown list 无法在表格列的下拉列表中显示动态值 - unable to show dynamic value in the dropdown list available in the table column 如何显示下拉列表中的选定选项 - How to display selected option from dropdown list 从下拉列表中选择其他选项时激活文本框 - activate a textbox on selecting other option from dropdown list 根据下拉列表中的选定选项显示数据库中的数据 - Show data from database depending on selected option in a dropdown list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM