【问题标题】:Dynamically adding fields on HTML Form在 HTML 表单上动态添加字段
【发布时间】:2015-04-29 12:58:47
【问题描述】:

我正在处理简单的表单,用户可以输入文本并将标签分配给(可选)文本,因为用户可以选择多个标签,所以我决定使用带有“多个”属性的选择标签。第一行的所有工作都按预期工作,但是当我动态添加新元素时,所有多选框都不像第一行。

  1. 我正在使用 SumoSelect / BootStrap-Select JQuery 插件来使我的多选框看起来更好 (https://hemantnegi.github.io/jquery.sumoselect/)
  2. 使用 bootstrap-select 我设法添加了另一行,并且新添加行上的元素独立于上面的行工作,但它添加了 2 个这样的多选框。
  3. 使用 SumoSelect,新添加的多选框没有响应

解释起来有点复杂,下面是代码和截图

HTML 代码:

     <div class="tab-pane fade active in" id="summary">
       <br>
       <a href="#">
            <span id="add_something" class="glyphicon glyphicon-plus" title="add summary" aria-hidden="true"></span>
       </a>
       <br><br>
       <div id="something_tbl">
           <p>Something <span class="user_info" style="padding-left: 5px; color:gray;">Enter something...</span></p>
           <p>
               <ol>
                    <li style="padding-bottom: 5px;" id="something_pt">
                        <input type="text" style="width: 70%;" placeholder="enter something" id="something" name="something">
                        <select name="something_tags" multiple size="5"
                                id="lbl_picker" class="selectpicker_1">
                            <option value="1">fun</option>
                            <option value="2">boring</option>
                            <option value="3">hehe</option>
                            <option value="4">lol</option>
                            <option value="5">xyz</option>
                        </select>
                    </li>
               </ol>
           </p>
       </div>

JQuery/JavaScript:

$("#add_something").on('click', function() {
  $('#something_tbl ol li#something_pt:first').clone().insertAfter('#something_tbl ol li#something_pt:last');
  $("#something_tbl ol li#something_pt:last #something").val('')
  var elem = $('#something_tbl ol li#something_pt:last');
  elem.append('<a href=\"#\"><span id=\"remove_summary\" title=\"Remove\" class=\"glyphicon glyphicon-minus\" style=\"padding-left: 5px;\"></span></a>');
  add_lbl_picker();
  elem.children('a').click(remove_summary);
  return false;
});    

var remove_summary = function() {
    var tbl_length = $('#something_tbl ol li#summary_pt').length;
    if (tbl_length > 1) {
        $(this).parents('li').remove();
    }
    return false;
};

var add_lbl_picker = function() {
    var summary_tbl_length = $('#something_tbl ol li#something_point').length;
    $('#something_tbl ol li#something_point:last select#lbl_picker').attr("class", "selectpicker_" + summary_tbl_length);
    $('.selectpicker_' + summary_tbl_length).SumoSelect();
};

$('.selectpicker_1').SumoSelect();

图片:

正如您在上面看到的,在第一次“多选”中选择的内容被复制到所有行,除了第一行,所有其他多选都不起作用!

注意:多选最多有 5 个值,并且值是静态加载的

【问题讨论】:

    标签: jquery html multi-select sumoselect.js


    【解决方案1】:

    我认为您的代码在正确使用属性“class”和“id”方面存在一些混淆。 对于您的脚本,下面的代码可以完成这项工作:

    <div class="tab-pane fade active in" id="summary">
           <br>
           <a href="#">
                <span id="add_something" class="glyphicon glyphicon-plus" title="add summary" aria-hidden="true">lkjlkjk</span>
           </a>
           <br><br>
           <div id="something_tbl">
               <p>Something <span class="user_info" style="padding-left: 5px; color:gray;">Enter something...</span></p>
               <p>
                   <ol>
                        <li style="padding-bottom: 5px;" class="something_pt">
                            <input type="text" style="width: 70%;" placeholder="enter something" class="something" name="something">
                            <select name="something_tags" multiple size="5" class="lbl_picker selectpicker_1">
                                <option value="1">fun</option>
                                <option value="2">boring</option>
                                <option value="3">hehe</option>
                                <option value="4">lol</option>
                                <option value="5">xyz</option>
                            </select>
                        </li>
                   </ol>
               </p>
           </div>
        <script>
        $("#add_something").on('click', function() {    
            something_pt_content.clone().insertAfter('.something_pt:last');
            $('.remove_summary:last').click(function(){
                if ($('.something_pt').length > 1) {
                    $(this).parents('li').remove();
                }   
            })
            $('.selectpicker_1').SumoSelect();
        });    
        var something_pt_content = $('.something_pt').clone().append('<a href=\"#\"><span title=\"Remove\" class=\"remove_summary glyphicon glyphicon-minus\" style=\"padding-left: 5px;\">oijojoj</span></a>');
        $('.selectpicker_1').SumoSelect();  
        </script>
    

    【讨论】:

    • 我尝试了上面的代码,但它没有按预期工作,你能在 jfiddle 上试试并在这里分享吗(我知道要求你花时间在 jfiddle 上设置它是不公平的,但如果它你有可能花那么多时间),虽然我会继续努力:)
    【解决方案2】:

    我想你在哪里写的:

    var add_lbl_picker = function() {
        var summary_tbl_length = $('#something_tbl ol li#something_point').length;
        $('#something_tbl ol li#something_point:last select#lbl_picker').attr("class", "selectpicker_" + summary_tbl_length);
        $('.selectpicker_' + summary_tbl_length).SumoSelect();
    };
    

    你的意思是:

    var add_lbl_picker = function() {
        var summary_tbl_length = $('#something_tbl ol li#something_pt').length;
        $('#something_tbl ol li#something_pt:last select#lbl_picker').attr("class", "selectpicker_" + summary_tbl_length);
        $('.selectpicker_' + summary_tbl_length).SumoSelect();
    };
    

    我将 something_point 更改为 something_pt

    【讨论】:

    • 感谢 Filipe,这只是我忘记删除实际字段名称的 1 个地方。我已经检查过,名称在 html 和 JS 中是正确的.. 但会继续尝试..
    猜你喜欢
    • 2011-08-31
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2019-04-19
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多