gpt4 book ai didi

javascript - 删除两个 html 元素之间的空格 : textbox and glyphicon

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:14 26 4
gpt4 key购买 nike

我想在 html 表单中动态添加行。但是,我有一个小问题:我的表格是:

<html lang="en">
<head>
<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<!-- Javascript -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
<script src="scripts.js"></script>
</head>

<body>
<div id = "mydiv" class="controls multi-field-wrapper row">
<div class="multi-fields">
<div class="multi-field col-md-11 col-lg-11">
<input type="text" name="how_many[]" placeholder="How many?" class="col-md-6 col-lg-6" >
<input type="text" name="where_when[]" placeholder="When and Where?" class="col-md-5 col-lg-5 " >
</div>
<a href="#" class="col-md-1 col-lg-1" style='margin: 0; padding: 0; border: 0; float: left;'>
<span class="add-field glyphicon glyphicon-plus-sign"></span>
</a>
</div>
</body>
</html>

我的 jquery 脚本是

jQuery(document).ready(function() {
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
var remove = '<a href="#" style="padding-left:20px" class ="remove-field "><span class=" glyphicon glyphicon-minus-sign"></span></a>';

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

但是问题是加号字形离文本框很远,而减号字形离文本框很近,这是所需要的,如图所示。我怎样才能将加号字形从文本框移动到与减号字形相似的距离。

enter image description here

这应该非常好,但我是该领域的新手,您能否找出问题所在并提出解决方案。谢谢!

最佳答案

演示:https://jsfiddle.net/gurn5zh9/

首先将你的 a 标签 add new field 放入你的 main div

<div id = "mydiv" class="controls  multi-field-wrapper row">
<div class="multi-fields">
<div class="multi-field col-md-11 col-lg-11">
<input type="text" name="how_many[]" placeholder="How many?" class="col-md-6 col-lg-6" >
<input type="text" name="where_when[]" placeholder="When and Where?" class="col-md-5 col-lg-5 " >
<a href="#" class="col-md-1 col-lg-1 addField" style='padding-left:20px'>
<span class="add-field glyphicon glyphicon-plus-sign"></span>
</a>
</div>
</div>
</div>

然后在您的脚本中将其从克隆中删除:

jQuery(document).ready(function() {
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
var remove = '<a href="#" style="padding-left:20px" class ="remove-field "><span class=" glyphicon glyphicon-minus-sign"></span></a>';

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

关于javascript - 删除两个 html 元素之间的空格 : textbox and glyphicon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37479387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com