简体   繁体   English

如何从动态填充的下拉列表中动态选择一个选项

[英]How to dynamically choose an option from a dynamically populated dropdown

I'm creating a company directory where you can create, read, update and delete entries about employees, departments, and locations.我正在创建一个公司目录,您可以在其中创建、读取、更新和删除有关员工、部门和位置的条目。 When updating a specific employee, accessed by a button on their employee's row:更新特定员工时,可通过其员工行上的按钮访问:

在此处输入图像描述

You get a modal:你得到一个模态:

在此处输入图像描述

The code for this modal is:此模式的代码是:

 <div class="modal fade" id="update_employee" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header update_header">
                <h5 class="modal-title" id="exampleModalLongTitle">Update Employee</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        <div id="updatingEmployee_problem" class="alert alert-danger alert-dismissible fade show" role="alert" style="display:none;">
        <p id="description_of_update_problem"></p>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        </div>
            <form>
            <div class="modal-body">
                <div id="update_this_id" hidden></div>
            <div class="form-group">
                <label for="name">First Name</label>
                <input class="form-control" id="update_fname">
            </div>
            <div class="form-group">
                <label for="name">Last Name</label>
                <input class="form-control" id="update_lname">
            </div>
            <div class="form-group">
                <label for="job_title">Job Title</label>
                <input class="form-control" id="update_job_title">
            </div>
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="update_email">
            </div>
            <div class="form-group">
                <label for="department">Department</label>
                <div class="row ml-1">
                    <select data-width="450px" title="Select department" class="selectpicker" id="departmentSearch4" onchange='possibleLocations("#departmentSearch4", "dependentLocation2")'></select>
                </div>
            </div>
            <div class="form-group">
                <label for="location">Location</label>
                <div class="row ml-1">
                    <select data-width="450px" title="Select location" id="dependentLocation2" class="selectpicker"></select>
                </div>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault" onclick="certainDecision('updateTheEmployee')">
                <label class="form-check-label" for="flexCheckDefault">
                    I am happy with the information provided.
                </label>
            </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button onclick="updateEmployee()" class="btn btn-primary" id="updateTheEmployee" disabled>Update</button>
            </div>
            </form>
        </div>
    </div>
</div>

Departments can have multiple locations.部门可以有多个位置。 Therefore, my location dropdowns are dynamically populated depending on which department is chosen.因此,我的位置下拉列表会根据选择的部门动态填充。

The code for this is:代码是:

function possibleLocations(department, id) {
$.ajax({
    type: 'POST',
    url: 'libs/php/locationOptions.php',
    data: {
        department: $(department + ' option:selected').text()
    },
    success: function (result) {

        while (document.getElementById(id).firstChild) {
            document.getElementById(id).removeChild(document.getElementById(id).lastChild);
        }

        for (let i = 0; i < result.length; i++) {
            var node = document.createElement("OPTION");
            var textnode = document.createTextNode(result[i].name);
            node.value = result[i].id;
            node.appendChild(textnode);
            document.getElementById(id).appendChild(node);
        }
        $('#' + id).selectpicker('refresh');
    }
})
}

I fill in this modal, by executing this code when clicking on the row's edit button:我通过单击行的编辑按钮时执行此代码来填写此模式:

 function update_this(ele) {
var row = ele.closest('tr');
var data = row.children;
var id = data[0].childNodes[0].data;
$('#update_this_id').text(id);
document.getElementById('update_fname').setAttribute('value', data[1].childNodes[0].data);
document.getElementById('update_lname').setAttribute('value', data[2].childNodes[0].data);
document.getElementById('update_job_title').setAttribute('value', data[3].childNodes[0].data);
document.getElementById('update_email').setAttribute('value', data[4].childNodes[0].data);
$('#departmentSearch4').val(data[5].childNodes[0].data).trigger('change');
$('#dependentLocation2').selectpicker('val', data[7].childNodes[0].data); << TRYING TO SELECT THE EMPLOYEE LOCATION FROM THE DYNAMIC LOCATION DROPDOWN
$('#update_employee').modal('show');
}

As departments can have multiple locations, I would like to automatically select the employee's location from the dynamic dropdown which is populated from the employee's department so that it looks 'filled out'.由于部门可以有多个位置,我想从员工部门填充的动态下拉列表中自动 select 员工的位置,使其看起来“已填写”。 Any ideas on how I can achieve this?关于如何实现这一目标的任何想法?

I figured out one solution!我想出了一个解决方案!

I can use a setTimeout so that the dropdown list has time to populate before I select the employee's location.我可以使用 setTimeout 以便在我 select 员工的位置之前填充下拉列表。 I did it with the following code:我用下面的代码做到了:

function update_this(ele) {
var row = ele.closest('tr');
var data = row.children;
var id = data[0].childNodes[0].data;
$('#update_this_id').text(id);
document.getElementById('update_fname').setAttribute('value', data[1].childNodes[0].data);
document.getElementById('update_lname').setAttribute('value', data[2].childNodes[0].data);
document.getElementById('update_job_title').setAttribute('value', data[3].childNodes[0].data);
document.getElementById('update_email').setAttribute('value', data[4].childNodes[0].data);
$('#departmentSearch4').val(data[5].childNodes[0].data).trigger('change');
setTimeout(function () { $('#dependentLocation2').selectpicker('val', data[7].childNodes[0].data) }, 1);
$('#update_employee').modal('show');
  }

I would still love anyone else's ideas though of how to solve it!尽管如何解决它,我仍然会喜欢其他人的想法!

暂无
暂无

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

相关问题 在动态填充的下拉列表中预选第一个选项 - Preselect first option in dynamically populated dropdown list 选择一个选项后,动态禁用下拉菜单 - Disable dropdown menu dynamically after choose an option 如何在bootstrap下拉列表中获取Selected元素,其中下拉列表是动态填充的 - How to get Selected element in bootstrap dropdown, where dropdown is dynamically populated 没有选项<select>从 JS 动态填充时的下拉列表 - No options in <select> dropdown when dynamically populated from JS 如何将动态选择的选项添加到动态填充的 select 列表中? - How to add dynamic selected option to a dynamically populated select list? 如何在下拉菜单中动态 select 选项? - How to dynamically select option in dropdown menu? 选择的下拉搜索不适用于动态填充的选项 - Chosen dropdown search is not working for dynamically populated options 从数据库填充的下拉列表中选择选项时,如何动态加载文本字段中的数据? Laravel Ajax - How to load data in text fields dynamically when selecting an option from drop down which is populated from database? Laravel Ajax 如何使用 PHP 或 Jquery 从动态创建的下拉列表中获取所选选项的 ID? - how I can get the ID of the selected option from a dynamically created dropdown using PHP, or Jquery? 如何从多选下拉列表中选择选项值时动态生成文本框 - how to dynamically generate text boxes on selecting option values from multi select dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM