如何使用带有Bootstap的javascript动态添加文件?

时间:2015-06-10 08:17:29

标签: javascript twitter-bootstrap

我在这里动态添加了代码Add fields dynamically - link

当我尝试使用此代码洞察我的表单类<form class="form-horizontal" action="create.php" method="post">时它不会起作用但是当从表单类中提取它时它将起作用我知道必须更改 java脚本但我很新 JS 所以请帮助我改变

这是我的表格:

<body>
  <form class="form-horizontal"  action="create.php"  method="post">
    <div class="form-group">
      <label for="des" class="col-sm-2 control-label">To Address</label>
      <div class="col-sm-8">
        <div class="controls"> 
          <form role="form" autocomplete="off">
            <div class="entry input-group ">
              <input class="form-control"name="fields[]"type="text"     placeholder="To....." />
              <span class="input-group-btn">
              <button class="btn btn-success btn-add"type="button">
              <span class="glyphicon glyphicon-plus"></span> </button> </span>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div class="form-group">
      <input name="submit" class="btn btn-success" type="submit" value="Save"   id="search"/>
    </div>
  </form>
</body>

myscript:

<script>
$(function()
{
    $(document).on('click', '.btn-add', function(e)
    {
        e.preventDefault();

        var controlForm = $('.controls form:first'),
            currentEntry = $(this).parents('.entry:first'),
            newEntry = $(currentEntry.clone()).appendTo(controlForm);

        newEntry.find('input').val('');
        controlForm.find('.entry:not(:last) .btn-add')
            .removeClass('btn-add').addClass('btn-remove')
            .removeClass('btn-success').addClass('btn-danger')
            .html('<span class="glyphicon glyphicon-minus"></span>');
    }).on('click', '.btn-remove', function(e)
    {
        $(this).parents('.entry:first').remove();

        e.preventDefault();
        return false;
    });
});

</script>

2 个答案:

答案 0 :(得分:0)

你在HTML标记中犯了一个错误,在另一个表单中添加了一个表单,我看了你的引用,你会发现你确实犯了错误。

<div class="container">
<div class="row">
    <div class="control-group" id="fields">
        <label class="control-label" for="field1">Nice Multiple Form Fields</label>
        <div class="controls"> 
            <form role="form" autocomplete="off">
                <div class="entry input-group col-xs-3">
                    <input class="form-control" name="fields[]" type="text" placeholder="Type something" />
                    <span class="input-group-btn">
                        <button class="btn btn-success btn-add" type="button">
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>
                    </span>
                </div>
            </form>
        <br>
        <small>Press <span class="glyphicon glyphicon-plus gs"></span> to add another form field :)</small>
        </div>
    </div>
</div>

因此,如果您想添加POST方法,可以将其添加到表单标记中,而不是创建新方法。

希望这有帮助。

答案 1 :(得分:0)

这里我错误地添加了表单洞察,从而添加了多个字段的简单方法:

<label for="des" class=" control-label">Description</label>
         <div class="multi-field-wrapper ">
           <div class="multi-fields">
             <div class="multi-field">
             <div class="col-sm-8">
               <input id="des" type="text" class="form-control" name="descrip[]"></div>
               <span class="remove-field">
                 <button class="btn btn-success btn-add" type="button">
                  <span class="glyphicon glyphicon-plus"></span></button></span>
             </div>
             </div>
         <button type="button" class="add-field">Add field</button>
        </div>

脚本功能:

 $('.multi-field-wrapper').each(function() {
    var $wrapper = $('.multi-fields', this);
    $(".add-field", $(this)).click(function(e) {
        $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
    });
    $('.multi-field .remove-field', $wrapper).click(function() {
        if ($('.multi-field', $wrapper).length > 1)
            $(this).parent('.multi-field').remove();
    });
});