在 jquery 对话框和帖子之后添加新选项到下拉列表

发布于 2024-08-29 17:47:01 字数 1615 浏览 3 评论 0原文

我有一份用于签订分包合同的表格。在这张表格上,我有系统中所有公司的下拉列表。旁边是一个“创建公司”按钮。此按钮将打开一个 jquery 对话框,允许用户创建新公司。对话框关闭后,需要将新公司添加到下拉列表中并进行选择。如果我刷新,它就在那里,但我需要在不刷新表单的情况下执行此操作 b/c 我不希望用户丢失他们在其他字段中输入的所有内容。我不知道如何执行此操作,因为我没有新公司的 guid/company_id。

我的 jquery 对话框:

 $('#popupCreateCompany').dialog(
                {
                    autoOpen: false,
                    modal: true,
                    buttons:
                    {
                        'Add': function() {
                            var dialog = $(this);
                            var form = dialog.find('input:text, select');
                            $.post('/company/create', $(form).serialize(), function() {
                                dialog.dialog('close');
                            })
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });


        $("#create-company").click(function() {
            $('#popupCreateCompany').dialog('open');
        });

公司字段:

<label for="company">Company:</label>
            <%= Html.DropDownList("company", Model.SelectCompanies, "** Select Company **") %>
            <%= Html.ValidationMessage("Company", "*") %>
            <button type="button" id="create-company" >Create Company</button>

SelectList 声明:

SelectCompanies = new SelectList(subcontractRepository.GetPrimaryCompanies(), "company_id", "company_name", Subcontract.company);

I have a form to enter subcontracts. On this form I have a dropdownlist of all companies in the system. Next to it is a button "Create Company". This button opens a jquery dialog which allows the user to create a new company. Once the dialog closes, the new company needs to be added to the dropdownlist and selected. If I refresh, it's there, but I need to do it without refreshing the form b/c I don't want the user to loose everything that they've entered into the other fields. I'm not sure how to do this b/c I don't have the guid/company_id of the new company.

My jquery dialog:

 $('#popupCreateCompany').dialog(
                {
                    autoOpen: false,
                    modal: true,
                    buttons:
                    {
                        'Add': function() {
                            var dialog = $(this);
                            var form = dialog.find('input:text, select');
                            $.post('/company/create', $(form).serialize(), function() {
                                dialog.dialog('close');
                            })
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });


        $("#create-company").click(function() {
            $('#popupCreateCompany').dialog('open');
        });

Company field:

<label for="company">Company:</label>
            <%= Html.DropDownList("company", Model.SelectCompanies, "** Select Company **") %>
            <%= Html.ValidationMessage("Company", "*") %>
            <button type="button" id="create-company" >Create Company</button>

SelectList declaration:

SelectCompanies = new SelectList(subcontractRepository.GetPrimaryCompanies(), "company_id", "company_name", Subcontract.company);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-09-05 17:47:01

我得到了它。我随指南返回了一个 Json。

            $('#popupCreateCompany').dialog(
                {
                    autoOpen: false,
                    modal: true,
                    buttons:
                    {
                        'Add': function() {
                            var dialog = $(this);
                            var form = dialog.find('input:text, select');
                            $.post('/company/create', $(form).serialize(), function(data) {
                                alert(data.Result);
                                if (data.Result == "success") {

                                    $('#company').append($('<option selected="selected"></option>').val(data.company_id).html(data.company_name));
                                };
                                dialog.dialog('close');
                            }, "json")
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });

I got it. I returned a Json with the guid.

            $('#popupCreateCompany').dialog(
                {
                    autoOpen: false,
                    modal: true,
                    buttons:
                    {
                        'Add': function() {
                            var dialog = $(this);
                            var form = dialog.find('input:text, select');
                            $.post('/company/create', $(form).serialize(), function(data) {
                                alert(data.Result);
                                if (data.Result == "success") {

                                    $('#company').append($('<option selected="selected"></option>').val(data.company_id).html(data.company_name));
                                };
                                dialog.dialog('close');
                            }, "json")
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文