首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改动态添加的div的ID

更改动态添加的div的ID
EN

Stack Overflow用户
提问于 2018-12-28 19:30:56
回答 1查看 334关注 0票数 0

我正在做一个网站,你可以在上面放上你的个人信息并制作一份简历。现在我想让你在你学习的课程中加入部分,但是我如何动态地添加更多的字段并更改元素的ID (+1),这样你就可以用jQuery获得naam_opleiding1,naam_opleiding2了?

代码语言:javascript
复制
<div class="input_fields_wrap">
    <input class="input-field" type="text" id="naam_opleiding" value="" placeholder="Naam opleiding"><br>
    <input class="input-field" type="text" id="naam_instituut" value="" placeholder="Naam instituut" /><br>
    <input class="input-field" type="text" id="startdatum" onfocus="(this.type='date')" value="" placeholder="Startdatum opleiding" /><br>
    <input class="input-field" type="text" id="einddatum" onfocus="(this.type='date')" value="" placeholder="Einddatum opleiding"/><br>
    <input class="input-field" type="text" id="overige_informatie"  value="" placeholder="Overige informatie"/><br>

        <button class="button add_field_button" id="opleiding_toevoegen" value="Opleiding toevoegen">Opleiding toevoegen</button>


$(wrapper).append('<div><br><br>' +
                    '<input class="input-field" name="mytext[]" type="text" id="naam_opleiding" value="" placeholder="Naam opleiding"><br>\n' +
                    '                        <input class="input-field" name="mytext[]" type="text" id="naam_instituut" value="" placeholder="Naam instituut" /><br>\n' +
                    '                        <input class="input-field" name="mytext[]" type="text" id="startdatum" value="" placeholder="Startdatum" /><br>\n' +
                    '                        <input class="input-field" name="mytext[]" type="text" id="einddatum" value="" placeholder="Einddatum"/><br>\n' +
                    '                        <input class="input-field" name="mytext[]" type="text" id="overige_informatie"  placeholder="Overige informatie"/><br>\n' +
                    '                        <a href="#" class="remove_field">Verwijder opleiding</a></div>'); //add input box
            }
            $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
                e.preventDefault(); $(this).parent('div').remove(); x--;
            })
        });
EN

回答 1

Stack Overflow用户

发布于 2018-12-28 19:46:45

下面的代码将允许您为多个输入组添加任意数量的额外输入。

输入组需要包装在一个带有类.input-wrapper的div中,并设置属性placeholdername,这些属性用于自动创建输入属性(包括唯一的id)。

delete按钮总是删除与选择模式匹配的最后一个输入,这样当您保存索引将按顺序排列的信息时。

出于演示的目的,我对代码进行了一些简化,并对其进行了完整的注释。

如果这不是你想要的让我知道

演示

代码语言:javascript
复制
// Add click event to .add-input buttons
$(document).on("click", ".add-input", function() {

  // Move up DOM tree to nearest wrapper
  el = $(this).closest(".input-wrapper");

  // Store name and placeholder for group
  name = el.attr("name");
  placeholder = el.attr("placeholder");

  // Count number of existing inputs by checking which have an id that starts with wrapper name
  // Using name here, in addition to input, so that you could add other inputs into the group wrapper if needed
  // You may want to switch .children to .find if you want to add more wrappers
  count = el.children("input[id^=" + name + "]").length;

  // Add to index
  next = parseInt(count + 1);

  // Append input
  el.append("<input id='" + name + "-" + next + "' placeholder='" + placeholder + " " + next + "'>");


});

// Add click event to .add-input buttons
$(document).on("click", ".delete-input", function() {

  // Move up DOM tree to nearest wrapper to get name
  name = $(this).closest(".input-wrapper").attr("name");
  
  // Move up DOM tree to nearest wrapper and then find last input that matches pattern and delete it
  $(this).closest(".input-wrapper").children("input[id^=" + name + "]").last().remove();

});
代码语言:javascript
复制
input,
label {
  width: 100%;
  margin-bottom: 4px;
}

.input-wrapper {
  border: 1px solid black;
  padding: 4px;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input name="name" placeholder="Name">
<input name="email" placeholder="Email">

<div name="course" class="input-wrapper" placeholder="Course">

  <label>Courses</label>

  <button class="add-input">Add</button>
  <button class="delete-input">Delete</button>

  <input id='course-1' placeholder='Course 1'>
  <input id='course-2' placeholder='Course 2'>

</div>

<div name="qualification" class="input-wrapper" placeholder="Qualification">

  <label>Qualifications</label>

  <button class="add-input">Add</button>
  <button class="delete-input">Delete</button>

</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53957894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档