【问题标题】:How to delete the div in a dynamically added sortable input boxes and indexing in front of the added input如何删除动态添加的可排序输入框中的 div 并在添加的输入前面建立索引
【发布时间】:2015-11-05 20:05:14
【问题描述】:

 $('#add').click(function() {
                  var x = $('<div class="ui-state-highlight"><input type="text" class="form-control" ng-model="input.field" placeholder="Name"/><input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="a"><span class="glyphicon glyphicon-move"></span></a><input type="button" class="form-control" class="Minus" value="-" />' + ($('.con div').length + 1) + '</div>');
                  x.appendTo('#form .con') // the length is not working here if I use it in the front of the div while declairng the variable.
                });

                $('.Minus').on('click', '#rem .Minus', function () {
                    $(this).closest(".fruit").remove();
                    });
         
                    $("span").sortable({
                    connectWith: ".con"
                }).disableSelection();
<!DOCTYPE html>
<html>

<head>
  <script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
  <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" />
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.js"></script>

</head>


<body>

 <div>
          
 <button id="add">add another option</button>
  <form id="form" class="form-inline">
    <span class="con">
        <div class="ui-state-highlight"><input type="text" class="form-control" ng-model="input.field" placeholder="Name"/>
                <input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="#">
          <span class="glyphicon glyphicon-move"></span><input type="button" class="form-control" class="Minus" value="delete" />
        </a>

</div>
       
    </span>
  </form>
   </div>
  </body>
  </html>
   
 

我正在尝试添加索引并删除单独添加的行。我尝试使用 div 的长度,但如果我将它附加到输入框的前面,它不会触发。这是 plunker http://plnkr.co/edit/4P6HWu9jVTmZhOnEhMdb?p=preview

【问题讨论】:

    标签: jquery html angularjs angular-ui-bootstrap


    【解决方案1】:

    给你:

    $('#add').click(function() {
      var x = $('<div class="ui-state-highlight">' + ($('.con div').length + 1) + '<input type="text" class="form-control" ng-model="input.field" placeholder="Name"/><input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="a"><span class="glyphicon glyphicon-move"></span></a><input type="button" class="form-control Minus" value="-" /></div>');
      x.appendTo('#form .con') // the length is not working here if I use it in the front of the div while declairng the variable.
    
    
    
    });
    
    $('.con').on('click', '.Minus', function() {
      $(this).parents(".ui-state-highlight").remove();
    });
    
    $("span").sortable({
      connectWith: ".con"
    }).disableSelection();
    <!DOCTYPE html>
    <html>
    
    <head>
      <script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
      <link rel="stylesheet" href="style.css" />
      <script src="script.js"></script>
      <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" />
      <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.js"></script>
    
    </head>
    
    
    <body>
    
      <div>
    
        <button id="add">add another option</button>
        <form id="form" class="form-inline">
          <span class="con">
            <div class="ui-state-highlight"><input type="text" class="form-control" ng-model="input.field" placeholder="Name"/>
                    <input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="#">
              <span class="glyphicon glyphicon-move"></span>
          <input type="button" class="form-control" class="Minus" value="delete" />
          </a>
    
      </div>
    
      </span>
      </form>
      </div>
    </body>
    
    </html>
    1. 你有两个 class 声明,所以我将它们合并得到 form-control Minus 类。
    2. 还修复了 .on() 处理程序的选择器。

    编辑: 您的新行标记由

    定义
    var x = $('<div class="ui-state-highlight"><input type="text" class="form-control" ng-model="input.field" placeholder="Name"/><input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="a"><span class="glyphicon glyphicon-move"></span></a><input type="button" class="form-control Minus" value="-" />' + ($('.con div').length + 1) + '</div>');
    

    要将索引移到前面,请将($('.con div').length + 1) 移到前面,如下所示:

    var x = $('<div class="ui-state-highlight">' + ($('.con div').length + 1) + '<input type="text" class="form-control" ng-model="input.field" placeholder="Name"/><input type="text" class="form-control" ng-model="input.value" placeholder="Email"/><a href="a"><span class="glyphicon glyphicon-move"></span></a><input type="button" class="form-control Minus" value="-" /></div>');
    

    【讨论】:

    • 你能告诉我我可以用什么让它在前面有编号吗?
    • 我已经更新了答案和代码sn-p。
    猜你喜欢
    • 1970-01-01
    • 2019-06-23
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多