javascript - Jquery ID申请

标签 javascript php jquery html css

我正在尝试使用 Jquery 和 PHP 创建一个动态订单表单,但卡住了。我有一个 Jquery 代码,onclick 添加了一组新的字段。同样在该 jquery 代码中,我将一些样式和 jquery 应用于其中一个字段。它是一个多选、可搜索的下拉列表。

但是这里有问题。我可以应用这种样式,以便它具有搜索栏等到第一个。后面的只是没有值或搜索栏的随机多列表。

这是我的代码:

$(document).ready(function() {
    var max_fields      = 100; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count

    //THIS WORKS!!
    var arrayFromPHP = ['foo', 'bar'];//<?php echo json_encode($a); ?>;
    $("#country" + x).select2({
        data: arrayFromPHP
    });

    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment

            //THIS DOESNT WORK!!
            var arrayFromPHP = ['foo', 'bar'];//<?php echo json_encode($a); ?>;
            $("#country" + x).select2({
                data: arrayFromPHP
            });

            $(wrapper).append('' +
                '<div>' +
                '<select id="country'+ x +'" multiple="multiple" style="width:300px;">' +
                '</select>' +
                '<a href="#" class="remove_field">Remove</a>' +
                '</div>' +
                '<div><input type="number" name="quantity'+ x +'" min="1" max="10"><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>

<div class="input_fields_wrap">
        <form method="post"><input type="submit" value="Add item" class="add_field_button"></form>
        <div>
            <select id="country1" multiple="multiple" style="width:300px;">
                <!-- Dropdown List Option -->
            </select>
        </div>
    </div>

所以在 Jquery 中,我在 caps rage 中放了 2 条评论来标记我的意思是行不通的。

截图:

初见

This on first sight

当我点击多选选项卡时,这有效。

When I click in the multiselect tab this works.

图片不言自明。只是搞砸了,如果我点击它,什么也不会发生。

picture speaks for itself. just messed up and if I click it nothing happends.

最佳答案

您在 HTML 元素存在之前初始化 select2:

$("#country" + x).select2({
  data: arrayFromPHP
});

$(wrapper).append('' +
  '<div>' +
  '<select id="country'+ x +'" multiple="multiple" style="width:300px;">' +
  '</select>' +
  'etc...');

select2 行应该出现在您使用append 方法创建元素之后。试试这个(注意我没有复制整个文本:这只是说明性的):

$(wrapper).append('' +
  '<div>' +
  '<select id="country'+ x +'" multiple="multiple" style="width:300px;">' +
  '</select>' +
  'etc...');

$("#country" + x).select2({
  data: arrayFromPHP
});

关于javascript - Jquery ID申请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47894495/

相关文章:

javascript - Ajax POST 到另一台服务器 - 克服跨域限制

php - JavaScript 导致 PHP 中无法修改头信息错误

c# - IE8提示下载json响应

javascript - 更新 jQuery 中 data-attr 的值

javascript - 使用循环推送到作为 Map 中的值的数组

javascript - 拖放后div大小增加

javascript - 我不想在拖动和排序时在行中包含列

php - 如何用后端数据填充 Zend_Layout 变量?

从同一个类的方法内部调用php oop方法

javascript - 使用 jQuery 选择出现多次的类