Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有动态输入字段的Jquery可排序div_Javascript_Jquery_Jquery Ui_Jquery Ui Sortable - Fatal编程技术网

Javascript 带有动态输入字段的Jquery可排序div

Javascript 带有动态输入字段的Jquery可排序div,javascript,jquery,jquery-ui,jquery-ui-sortable,Javascript,Jquery,Jquery Ui,Jquery Ui Sortable,我正在尝试对这些动态输入字段进行排序,我希望能够单独拖动它们,以便它们可以重新排序。现在,当我运行这段代码时,它不是将整行作为div处理,而是将div的每个元素都处理。我缺少什么 <!DOCTYPE html> <html> <head> <title>Add or Remove text boxes with jQuery</title> <script src="//code.jquery.com/jquery-1.10.2.

我正在尝试对这些动态输入字段进行排序,我希望能够单独拖动它们,以便它们可以重新排序。现在,当我运行这段代码时,它不是将整行作为div处理,而是将div的每个元素都处理。我缺少什么

<!DOCTYPE html>
<html>
<head>
<title>Add or Remove text boxes with jQuery</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<style type="text/css">
<!--
#main {
    max-width: 800px;
    margin: 0 auto;
}
-->
</style>
</head>
<body>
<div id="main">
  <h1>Add or Remove text boxes with jQuery</h1>
  <div class="my-form">
    <form role="form" method="post">
      <div class="text-box" id="text-box">
        <label for="box1">Box <span class="box-number">1</span></label>
        <input type="text" name="boxes[]" value="" id="box1" />
        <a class="add-box" href="#">Add More</a>
      </div>
      <p>
        <input type="submit" value="Submit" />
      </p>
    </form>
  </div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
  $('.my-form .add-box').click(function() {
    var n = $('.text-box').length + 1;
    if (5 < n) {
      alert('Stop it!');
      return false;
    }
    var box_html = $('<div id="text-box" class="text-box" id="text-box"><label for="box' + n + '">Box <span class="box-number">' + n + '</span></label> <input type="text" name="boxes[]" value="" id="box' + n + '" /> <a href="#" class="remove-box">Remove</a></div>');
    box_html.hide();
    $('.my-form .text-box:last').after(box_html);
    box_html.fadeIn('slow');
    return false;
  });

  $('.my-form').on('click', '.remove-box', function() {
    $(this).parent().css('background-color', '#FF6C6C');
    $(this).parent().fadeOut("slow", function() {
      $(this).remove();
      $('.box-number').each(function(index) {
        $(this).text(index + 1);
      });
    });
    return false;
  });

  $('.my-form').on('click', '.text-box:last', function() {
    var n = $('.text-box').length + 1;
    if (5 < n) {
      alert('Stop it!');
      return false;
    }
    var box_html = $('<div id="text-box" class="text-box" id="text-box"><label for="box' + n + '">Box <span class="box-number">' + n + '</span></label> <input type="text" name="boxes[]" value="" id="box' + n + '" /> <a href="#" class="remove-box">Remove</a></div>');
    box_html.hide();
    $('.my-form .text-box:last').after(box_html);
    box_html.fadeIn('slow');
    return false;
  });

  $(function() {
    $("#text-box").sortable({
      placeholder: "ui-state-highlight",
      helper: 'clone'
    });
    $("#text-box").disableSelection();
  });
});
</script>
</body>
</html>

使用jQuery添加或删除文本框
使用jQuery添加或删除文本框
方框1

jQuery(文档).ready(函数($){ $('.my form.add box')。单击(函数(){ var n=$('.text box')。长度+1; if(5
浏览您的HTML。注意你的缩进,这有助于保持事物的条理性和可读性。在任何情况下,都会有一个恶意关闭“”标记挂在与“添加更多”按钮相同的行上。您应该删除,看看这是否有任何影响。此外,您不需要将提交按钮包装在“p”标签内。谢谢sm1215。我知道是什么了。下面是jfiddle中的一个工作示例,请查看您的HTML。注意你的缩进,这有助于保持事物的条理性和可读性。在任何情况下,都会有一个恶意关闭“”标记挂在与“添加更多”按钮相同的行上。您应该删除,看看这是否有任何影响。此外,您不需要将提交按钮包装在“p”标签内。谢谢sm1215。我知道是什么了。下面是jfiddle中的一个工作示例