jQuery在提交之前将动态加载的Ajax选择列表的输入附加到另一个表单

时间:2022-12-07 00:07:31

For the question let me simplify my code:

对于这个问题,让我简化我的代码:

I have two html forms with id's of 'header' and 'existing'

我有两个html表单,ID为'header'和'existing'

In the 'existing' form I have a select element where the options list is loaded dynamically using Ajax depending on the result of a search - this works fine with the following jQuery:

在“现有”表单中,我有一个select元素,其中选项列表使用Ajax动态加载,具体取决于搜索结果 - 这适用于以下jQuery:

$(document).ready(function() {
    $('#article').keyup(function() {
        var getData = 'q=' + this.value + '&s=' + $('#supplierCode').val();
        $.getJSON('article.php', getData, function(j) {
            var options = '';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            }
            $('select#articleList').html(options);
        });

        return false;
    });
});

The search works and the list appears to be loaded correctly as part of the form. I then want the input data of this form to be combined with the data of the 'header' form before submitting. I use this jQuery:

搜索工作,列表似乎作为表单的一部分正确加载。然后,我希望在提交之前将此表单的输入数据与“标题”表单的数据相结合。我用这个jQuery:

$(document).ready(function() {
    $('#articleList').change(function() {
        $('#existing :input').not(':submit').clone().hide().appendTo('#header');
        $('#header').submit();
    });
});

But whatever I select from the options list the $_REQUEST data always shows the first item on the list as chosen.

但无论我从选项列表中选择什么,$ _REQUEST数据始终显示列表中的第一个项目。

I can add more code if this is necessary - but hopefully the esence of the problem is clear!

如果有必要,我可以添加更多代码 - 但希望问题的实现是明确的!

1 个解决方案

#1


1  

If you are to study the way Select2 Jquery plugin works,

如果你要研究Select2 Jquery插件的工作方式,

The plugin creates a list of items using <ul>s and stores the value of the chosen element in an <input>.

该插件使用

    创建项目列表,并将所选元素的值存储在 中。

I believe this is done, so as to workaround the issue that you are having with dynamically populated <select> lists.

我相信这已经完成,以便解决您使用动态填充的

You can do the same as a solution to your issue.

您可以像解决问题一样解决问题。

#1


1  

If you are to study the way Select2 Jquery plugin works,

如果你要研究Select2 Jquery插件的工作方式,

The plugin creates a list of items using <ul>s and stores the value of the chosen element in an <input>.

该插件使用

    创建项目列表,并将所选元素的值存储在 中。

I believe this is done, so as to workaround the issue that you are having with dynamically populated <select> lists.

我相信这已经完成,以便解决您使用动态填充的

You can do the same as a solution to your issue.

您可以像解决问题一样解决问题。