PHP / Bootstrap - 动态添加字段&提交表格

时间:2016-03-03 18:46:44

标签: javascript php jquery css twitter-bootstrap-3

我有点失落,并认为你们可能会有所帮助。

我想做什么: 我有一个表单,用户将动态添加字段,然后我希望用户提交该表单并在下一页显示值。但是用户必须选择一个下拉列表以显示他们在文本框中提交的数据类型。例如

--------------------------
- Drop Down -Text Box   -
--------------------------

--------------------------
-  INT        -    123   -
--------------------------
--------------------------
-  TEXT       -    ABC   -   +
--------------------------

表格提交后:

INT - 123
TEXT- ABC

有谁知道我怎么能这样做?

我的代码:

<div class="col-md-4">
            <div class="values">
                <label>Type:</label>
                <form method="post" action="values.php">
                    <div class="form-group multiple-form-group input-group">
                        <div class="input-group-btn input-group-select">
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                <span class="concept">int</span> <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" role="menu">
                                <li><a href="#int">int</a></li>
                                <li><a href="#text">text</a></li>
                            </ul>
                            <input type="hidden" class="input-group-select-val" name="contacts['type'][]" value="int">
                        </div>
                        <input type="text" name="contacts['value'][]" class="form-control">
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-success btn-add">+</button>
                        </span>
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
                </div>
            </div>


<script>
(function ($) {
    $(function () {

        var addFormGroup = function (event) {
            event.preventDefault();

            var $formGroup = $(this).closest('.form-group');
            var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
            var $formGroupClone = $formGroup.clone();

            $(this)
                .toggleClass('btn-success btn-add btn-danger btn-remove')
                .html('–');

            $formGroupClone.find('input').val('');
            $formGroupClone.find('.concept').text('int');
            $formGroupClone.find('.input-group-select-val').text('int');
            $formGroupClone.insertAfter($formGroup);

            var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
            if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) {
                $lastFormGroupLast.find('.btn-add').attr('disabled', true);
            }
        };

        var removeFormGroup = function (event) {
            event.preventDefault();

            var $formGroup = $(this).closest('.form-group');
            var $multipleFormGroup = $formGroup.closest('.multiple-form-group');

            var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
            if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) {
                $lastFormGroupLast.find('.btn-add').attr('disabled', false);
            }

            $formGroup.remove();
        };

        var selectFormGroup = function (event) {
            event.preventDefault();

            var $selectGroup = $(this).closest('.input-group-select');
            var param = $(this).attr("href").replace("#","");
            var concept = $(this).text();

            $selectGroup.find('.concept').text(concept);
            $selectGroup.find('.input-group-select-val').val(param);

        }

        var countFormGroup = function ($form) {
            return $form.find('.form-group').length;
        };

        $(document).on('click', '.btn-add', addFormGroup);
        $(document).on('click', '.btn-remove', removeFormGroup);
        $(document).on('click', '.dropdown-menu a', selectFormGroup);

    });
})(jQuery);

</script>

0 个答案:

没有答案