选择选项时以动态形式创建动态输入

时间:2018-09-19 17:12:03

标签: javascript jquery html5

我正在尝试创建一个动态表单,该表单在选择“验证选择”时会添加另一个字段,问题是当我在添加另一种表单时在“验证”中添加选项字段时,出现了我之前添加的选项,如何添加新表格并且不显示添加的选项?感谢您的帮助!

  $(document).ready(function() {

            $('.verificar-free').hide();
            $('.duplicate-free').hide();
            $('.optionRow').hide();

            var count = 2;

            //duplicate
            $('a.add-free').on('click', function() {
                //clone
                var row = $('.duplicate-free').clone();
                $(row).insertAfter('.aditional-box-free');
                $(row).show();

                //add new ids
                $(row).find('select').attr('id', 'select-free_' + count);
                //remove duplicate class
                $(row).removeClass('duplicate-free');

                //onchange of select
                $('select').on('change', function() {

                    var value = $(this).val();
                    var select = $(this).parent();

                    if (value == 1) {
                        $(select).siblings('.input-free').show();
                        $(select).siblings('.ocultar-free').hide();
                    } else {
                        $(select).siblings('.input-free').hide();
                    }
                    if (value == 2) {
                        $(select).siblings('.ocultar-free').show();
                        $(select).siblings('.verificar-free').show();
                    } else {
                        $(select).siblings('.verificar-free').hide();
                    }

                });
                
                //add option
                $(".addRow-free").click(function() {
                    var html = "<div class='option-free' id='" + count + "'><div class='form-group'><div class='input-group select'><input type='text' class='form-control' placeholder='Añade opción' /><span class='input-group-btn'><button class='btn btn-primary remove-option' type='button'><a class='remove-tipe' href='javascript: void(0)'><span class='glyphicon glyphicon-trash' style='color:white'></span></a></button></span></div></div></div>";

                    var form = $(html);

                    $(this).closest(".verificar-free").find(".optionRow-free").append(form);

                });
                count++;
            });

        });
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 
     <div class="aditional-questions text">
        <div class="aditional-box-free">
            <p class="aditional-text" for=""><b>Add Question</b>
                <a class="btn btn-primary agregar add-free" href="javascript: void(0)" type="button"><span></span>Add</a>
            </p>
        </div>
        <div class="duplicate-free all-free" style="text-align: center">
            <div class="box-question">
                <div class="row">
                    <label class="type-question-text" for="">Question Type</label>
                    <div class="col-md-12">

                        <select class="form-control select">
                                            <option value="1">Text</option>
                                            <option value="2">Verification</option>
                                           </select>
                    </div>
                    <div class="row ocultar-free">
                        <div class="col-md-12">
                            <label class="type-question-text" for="">Title</label>

                            <div class="form-group">
                                <input type="text" id="" class="form-control text general" placeholder="Question">
                            </div>
                        </div>
                    </div>
                    <!--aditional option-->
                    <div class="row verificar-free">
                        <div class="text">
                            <div class="col-md-6">

                            </div>
                        </div>
                        <div class="text option text" style="margin-top:10px;">
                            <a class="btn btn-primary addRow-free" href="javascript: void(0)" type="button"><span></span>Add Option</a>
                        </div>
                        <br>
                        <div class="form-group optionRow-free">

                        </div>
                    </div>
                </div>
                <br>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

如果您的问题我没错。

问题是您添加了相同(重复)的输入内容吗? (如果有多个问题)

更改此行

$(".optionRow-free").append(form);

收件人

$(this).closest(".verificar-free").find(".optionRow-free").append(form);