简体   繁体   English

在模式中编辑表单时,如何从依赖下拉列表中获取先前选择的选项?

[英]When editing a form in a modal, how do I get the previously selected option from a dependent dropdown?

I have two tables, one for customers and one for their vehicles.我有两张桌子,一张给顾客,一张给他们的车。 When submitting the form the vehicle is dependent on which customer you select.提交表格时,车辆取决于您的客户 select。 Now when I go to edit the form, I can select vehicles by changing the customer like so...现在当我 go 编辑表格时,我可以像这样更改客户 select 车辆...

$(document).on('change','#customeredit', function(){
        var customer_id = $(this).val();
        if(customer_id){
            $.ajax({
                type:'POST',
                url:'get-vehicle-from-customer.php',
                data:{'customer_id':customer_id},
                success:function(result){
                    $('#vehicleedit').html(result);

                }
            }); 
        }else{
            $('#vehicleedit').html('<option value="">Vehicle</option>');
        }
    });

but I cannot for the life of me figure out how to get it to automatically show the list of vehicles without changing the customer selection first.但我一生无法弄清楚如何让它自动显示车辆列表而不首先更改客户选择。 I know I'm missing something simple here but I'm new to this and could really use some direction.我知道我在这里遗漏了一些简单的东西,但我对此并不陌生,并且真的可以使用一些方向。 Thanks!谢谢!

i would define your ajax call as a function, then u can call it everywhere u want:我会将你的 ajax 调用定义为 function,然后你可以在任何你想要的地方调用它:

    var fx_load_customer = function() 
        {
          //var customer_id = $(this).val();
            var customer_id = $('#customeredit').val();
            if(customer_id){
                $.ajax({
                    type:'POST',
                    url:'get-vehicle-from-customer.php',
                    data:{'customer_id':customer_id},
                    success:function(result){
                        $('#vehicleedit').html(result);

                    }
                }); 
            }else{
                $('#vehicleedit').html('<option value="">Vehicle</option>');
            }
        }   

        $(document).on('change','#customeredit', function(){ fx_load_customer(); });
        fx_load_customer();

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

相关问题 如何获得先前选择的选项? - How to get previously selected option? 选择从选择下拉菜单中的选项时出现的模态 - modal appearing when option from select dropdown has been selected 如何从下拉菜单中选择选项? - how to get selected option from dropdown menu? 从下拉列表中单击相同选项时调用/触发功能 - Call / fire function when click on same option from dropdown list which was previously selected 如果选择了另一个选择下拉列表中的一个选项,如何显示选择下拉列表 - How do I display a select dropdown list when one option on another select dropdown is selected 如何从动态下拉选项中获取当前选定的选项值? - How to get current selected option value from dynamic dropdown option? 从动态相关下拉列表中获取所选选项 - 使用 Flask (Python) - Get selected option from dynamic dependent dropdown list - using Flask (Python) 在下拉菜单1中选中后,从下拉菜单2中隐藏选项 - Hide option from dropdown 2 when selected in dropdown 1 如果没有从下拉列表中选择任何选项,如何阻止表单提交 - How to prevent form submission if there is not any option selected from dropdown 当从下拉菜单中选择选项时,如何使用jQuery将所选选项隐藏到另一个下拉菜单中 - When selected option from dropdown then how to hide that selected option into another dropdown using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM