gpt4 book ai didi

javascript - 如何使用 Javascript/Jquery 单击删除按钮后连续获取文件输入的所有 id

转载 作者:行者123 更新时间:2023-12-03 09:23:49 25 4
gpt4 key购买 nike

我需要一个帮助。我有一个文件字段和一个 + 按钮。当用户单击 + 按钮时,会生成另一个文件字段和一个 - 按钮与第一个字段字段一起创建,依此类推。这里所有 id 都会动态生成并相应递增。我在下面解释我的代码。

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon">
<link rel="apple-touch-icon" href="img/apple_icons_57x57.png">
<link rel="apple-touch-icon" sizes="72x72" href="img/apple_icons_72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="img/apple_icons_114x114.png">
<!--/ metas -->

<!-- styles -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/layerslider.css">
<link rel="stylesheet" type="text/css" href="css/fullwidth/skin.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="css/owl.carousel.css">
<link rel="stylesheet" type="text/css" href="css/jquery.fancybox.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" type="text/css" href="css/color-blue.css">
<link rel="stylesheet" type="text/css" href="css/intlTelInput.css">
</head>

<body>
<div class="col-md-6 bannerimagefile bmargindiv1">
<label for="expcerti" accesskey="B"><span class="required">*</span> Publication/Papers Upload your publication/papers certificate.</label>
<ol id="expOl">
<li>
<div class="totalaligndiv">
<div class="col-md-10 padding-zero bannerimagefilenew bmargindiv1">
<input type="file" class="filestyle" data-size="lg" name="expcerti" id="expcerti" />
</div><div class="col-md-2 padding-zero bmargindiv1">
<button type="button" class="btn btn-success btn-sm " id="Expadd">+</button>
<button type="button" class="btn btn-danger btn-sm" id="Expminus" style="display:none;" >-</button>
</div>
<div class="clearfix"></div>
</div>
</li>
</ol>
</div>
<!--end_how_it_work_demo_div-->

<!-- scripts -->
<script type="text/javascript" src="js/jquery.min.js"></script>

<script type="text/javascript" src="js/bootstrap-filestyle.min.js"></script>

<!--/ Superscrollorama -->


<!--/ scripts -->
<script>
$(document).ready(function () {
var i=1;
$(document).on('click','.btn-success', function () {
$.getScript("js/bootstrap-filestyle.min.js");
$('#expOl').append("<li><div class='totalaligndiv'><div class='col-md-10 padding-zero bannerimagefilenew bmargindiv1'><input type='file' class='filestyle' data-size='lg' name='expcerti' id='expcerti"+i+"' ></div><div class='col-md-2 padding-zero bmargindiv1'><button type='button' class='btn btn-success btn-sm ' id='Expadd'>+</button><button type='button' class='btn btn-danger btn-sm' id='minus' style='display:none'>-</button></div><div class='clearfix'></div></div></li>");
//$('.filestyle').attr("id","expcerti"+i);
$(this).css('display', 'none');
$(this).siblings("button.btn-danger").css('display', 'block');
i++;
});
$(document).on('click','.btn-danger', function () {
console.log('delete');
$(this).closest("li").remove();
});
});
</script>
</body>
</html>

在上面的代码中,假设第一个字段 id 为 expcerti,第二个字段为 expcerti1,依此类推。如果用户为这些字段创建 5 个字段,则 id 将为 expcerti,expcerti1,expcerti2,expcerti3,expcerti4。我需要,如果用户从这 5 个字段 id 中删除任何字段,则应该再次连续出现,我对此进行了解释。假设用户删除了 3 个字段,则 id 应该再次像这样(expcerti,expcerti1,expcerti2,expcerti3)。我正在使用 Jquery。请帮助我解决我的问题。

最佳答案

如果目标是对 type = file 的输入 ID 进行重新排序,那么最好的选择是编写一个小函数,在每次需要重命名任何输入 ID 时重命名所有输入 ID。我认为这对你有用:

function reorderFileInputs(){
var counter = 0;
var baseString = 'expcerti';
var fileInputs = $(document).find('input[type=file]'); //RETURNS ARRAY OF FILE INPUTS
$.each(fileInputs, function(){
if(counter == 0){
this.id = baseString;
this.name = baseString;
} else{
this.id = baseString + counter;
this.name = baseString + counter;
}

counter ++;
});
}

关于javascript - 如何使用 Javascript/Jquery 单击删除按钮后连续获取文件输入的所有 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31750188/

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