【问题标题】:Dynamically generated input box resize and refresh position and size动态生成的输入框resize和刷新位置和大小
【发布时间】:2017-01-26 18:31:29
【问题描述】:

我有一个表单,该表单内部是一个按钮和一个输入字段,我希望当有人单击“添加更多字段”按钮时会有一个新的动态生成的输入框。我尝试使用posted code in jsfiddle,这很有效,但我需要像这张照片一样:

所以我需要让输入字段的位置如下图所示: - 如果在一行中添加一个框,则它的宽度为:200px; - 如果连续添加两个框,则框的宽度为:100px - 如果你看右边的图片;我需要可以通过单击它来删除一个框,然后在删除一个框时。我希望所有的盒子再次对齐到相同的位置(这里盒子 3 被移除,4 占据它的位置,6 变小,5 向左移动。)。

我尝试在 jsfiddle 上使用下面的代码,但我认为我需要一些 css 和 jquery:

$(document).ready(function() {
  var max_fields      = 10; //maximum input boxes allowed
  var wrapper         = $(".input_fields_wrap"); //Fields wrapper
  var add_button      = $(".add_field_button"); //Add button ID

  var x = 1; //initlal text box count
  $(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    if(x < max_fields){ //max input box allowed
      x++; //text box increment
      $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
    }
  });

  $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    e.preventDefault(); $(this).parent('div').remove(); x--;
  })
});

~谢谢

【问题讨论】:

  • 请不要忽略指出小提琴链接应在问题中附上代码的警告框
  • 那么什么是“行”?而当 3 被删除时,这意味着 4 会自动切换到另一行吗?通过什么逻辑 4 向上移动,而不是与 5 一起停留在当前行?
  • 一行就像在表格中(一条水平线包含一个长盒子或两个小盒子)。是的,当删除 3 时,它需要自动切换到另一行并从下一行(当前为 4)移动到位置 3。我正在构建表单,在单击类别时动态添加输入框,然后添加新框或删除选定框..所以如果我在删除 item3 时留下空的地方..这在表格上看起来很丑而且不专业。所以我需要用 4 中的框填充已移除 3 的位置。

标签: javascript jquery html css


【解决方案1】:

TL;DR:https://jsfiddle.net/6b24z4j7/4/

所以基本上你需要应用一些 nth-child css 逻辑来维护输入的视图顺序。我对 jQuery 做了一些小改动,但它仍然保持你的逻辑。在小提琴示例中,我制作了占位符属性来展示事物的工作方式(您可以稍后将其删除)。

CSS:

*{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.form{
  width: 400px;
  max-width: 90%;
  margin: 20px auto;
  padding: 20px;
  background-color: #fff;
  border: 1px solid #e2e2e2;
  border-radius: 15px;
}
.form__actions{
  text-align: center;
}
.form__button{
  display: table;
  margin: 0 auto 15px;
  padding: 6px;
  color: #fff;
  background: #3498db;
  border: none;
  border-radius: 4px;
  cursor: pointer;

  -webkit-appereance: none;
  -moz-appereance: none;
  -ms-appereance: none;
  appereance: none;  
}
.form__row{
  margin: 0 -10px;
}
.form__row:before, .form__row:after{
  content: '';
  display: table;
  clear: both;
}
.form__field{
  float: left;
  padding: 0 10px;
  margin: 0 0 25px;
  position: relative;
}
.form__field:nth-child(2n-1){
  width: 50%;
}
.form__field:nth-child(2n){
  width: 50%;
}
.form__field:nth-child(3n){
  width: 100%;
}
.form__field:hover .form__removeField{
  opacity: 1;
}
.form__removeField{
  position: absolute;
  top: -10px;
  right: 20px;
  width: 20px;
  height: 20px;
  opacity: 0;
  background: #e74c3c;
  color: #fff;
  line-height: 20px;
  text-align: center;
  font-size: 14px;
  border-radius: 50%;
  cursor: pointer;

  -webkit-transition(all .4s ease);
  -moz-transition(all .4s ease);
  -ms-transition(all .4s ease);
  transition(all .4s ease);
}
.form__input{
  display: block;
  width: 100%;
  background: #fff;
  padding: 0 10px;
  line-height: 32px; 
  border: 1px solid #888;
  border-radius: 5px;
  -webkit-appereance: none;
  -moz-appereance: none;
  -ms-appereance: none;
  appereance: none;
} 

JS(jQuery):

$(document).ready(function() {
  var max_fields      = 10; //maximum input boxes allowed
  var wrapper         = $(".input_fields_wrap"); //Fields wrapper
  var add_button      = $(".add_field_button"); //Add button ID
    var count           = $(".input_fields_wrap").find('.form__field').length; //or write a static number if you know how many fields you will have

  $(add_button).click(function(e){ //on add input button click

    e.preventDefault(); 
    if(count < max_fields){ //max input box allowed
      count++; //text box increment
      $(wrapper).append('<div type="text" class="form__field"><input type="text" class="form__input" placeholder="'+count+'"><div class="form__removeField remove_field">x</div></div>'); //add input box
    }
  });

  $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    e.preventDefault(); 
    $(this).parent('div').remove(); 
    count--;
  })
});

HTML:

<form class="form">
  <div class="form__actions">
    <button class="form__button add_field_button">Add input</button>
  </div>
  <div class="form__row input_fields_wrap">
    <div type="text" class="form__field">
      <input type="text" class="form__input">
      <div class="form__removeField remove_field">x</div>
    </div>
    <div type="text" class="form__field">
      <input type="text" class="form__input">
      <div class="form__removeField remove_field">x</div>
    </div>
    <div type="text" class="form__field">
      <input type="text" class="form__input">
      <div class="form__removeField remove_field">x</div>
    </div>   
  </div>
</form>

Hopefilly,我理解您的输入创建/删除背后的逻辑。

【讨论】:

  • 谢谢...这正是我所需要的...但我发现我描述了一件错误的事情...所以如果您可以通过少量修改来更新代码...我忘记描述了你需要在一行中有两个盒子......所以如果我们有 3 个盒子(第一行:每 100 像素 2 个盒子;第二行:1 盒子......如果我们有 4 个盒子,则为 200 像素(第一行:每 100 像素 2 个盒子;第二行:每个 100 像素 2 个框;....如果您有 5 个框(第一行:每个 100 像素 2 个框;...第二行:每个 100 像素 2 个框;第三行一个框宽度:200 像素;)如果我不清楚详细说明,请重播然后我将绘制油漆示例。
  • 在 css 部分更改 ".form__field:nth-child(3n){ width: 100%; }" 为 ".form__field:last-child:nth-child(2n-1){ width: 100%;}"
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 2018-07-15
相关资源
最近更新 更多