javascript - 在 jquery 中动态添加内容问题
问题描述
我通过单击按钮将其附加到 div 来添加块内容。问题是,它正在按钮下方添加内容Add Section
。它应该在Add Section
按钮上方添加内容,即注释的位置。我的代码如下:
有人可以帮忙吗?我希望div class="g"
每次单击Add Section
按钮时都将内容添加到上方。
<div class="a">
<div class="b">
<div class="c">
<input type="text" id="user-title-1" class="d" value="user 1">
<button class="mdl-button mdl-js-button edit-title-btn">
</button>
</div>
<div class="e">
<ul>
for (int j = 1; j <= numOfCheckboxes; j++)
{
<li>
<input type="checkbox" id="user-@j-1">
<label for="user-@j-1"><i class="icon-tick" disabled></i></label>
</li>
}
}
</ul>
<div class="f">
<small class="_availability">
<span class="title"> Section 1 </span>
</small>
<button type="button" class="mdl-button btn-show js-show-supplier">
<span class="show">Show <i class="icon-show"></i></span>
</button>
</div>
</div>
<!-- New section to be added here
//
-->
<div class="g">
<button type="button" class="mdl-button add-Section-btn js-add-Section">
AddSection
</button>
<button type="button" class="mdl-button secondary-btn js-save">
Save
</button>
</div>
</div>
</div>
<script>
$(document.body).on('click', 'button.js-add-Section', function () {
var content = `<div class="e">
<ul>
for (int j = 1; j <= numOfCheckboxes; j++)
{
<li>
<input type="checkbox" id="user-@j-1">
<label for="user-@j-1"><i class="icon-tick" disabled></i></label>
</li>
}
}
</ul>
<div class="f">
<small class="_availability">
<span class="title"> Section 1 </span>
</small>
<button type="button" class="mdl-button btn-show js-show-supplier">
<span class="show">Show <i class="icon-show"></i></span>
</button>
</div>
</div>`
$(this).closest('.b').append(content);
});
</script>
解决方案
只需将append()
功能更改为insertBefore()
这是一个工作示例
$(document.body).on('click', 'button.js-add-Section', function() {
var content = `<div class="e">
<ul>
<li>
<input type="checkbox" id="user-@j-1">
<label for="user-@j-1"><i class="icon-tick" disabled></i></label>
</li>
</ul>
<div class="f">
<small class="_availability">
<span class="title"> Section 1 </span>
</small>
<button type="button" class="mdl-button btn-show js-show-supplier">
<span class="show">Show <i class="icon-show"></i></span>
</button>
</div>
</div>`;
$(content).insertBefore($(this).closest('.g'));
});
$(document.body).on('click', 'button.js-show-supplier', function() {
alert('Clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="a">
<div class="b">
<div class="c">
<input type="text" id="user-title-1" class="d" value="user 1">
<button class="mdl-button mdl-js-button edit-title-btn">
</button>
</div>
<div class="e">
<ul>
<li>
<input type="checkbox" id="user-@j-1">
<label for="user-@j-1"><i class="icon-tick" disabled></i></label>
</li>
</ul>
<div class="f">
<small class="_availability">
<span class="title"> Section 1 </span>
</small>
<button type="button" class="mdl-button btn-show js-show-supplier">
<span class="show">Show <i class="icon-show"></i></span>
</button>
</div>
</div>
<div class="g">
<button type="button" class="mdl-button add-Section-btn js-add-Section">
AddSection
</button>
<button type="button" class="mdl-button secondary-btn js-save">
Save
</button>
</div>
</div>
</div>
编辑
你的意思是这样的吗?
$(".main").on('change', '.js-cars-itemas [type="checkbox"]', function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.cars').find('.js-cars-itemas').each(function() { //Loop every js-cars-item
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".cars").find(".cars-itemas input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
$('.js-add-category').click(function() {
var categoryContent = `<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
</div> <br>`
$('.main').append(categoryContent);
});
$(document.body).on('click', 'button.js-add-section', function() {
var sectionContent = `<div class="cars-item js-cars-itemas">
<ul>
<li>
<input type="checkbox" id="car-1-6>
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6>
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6>
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
</ul> </div>`
$(this).closest('.cars').append(sectionContent);
});
$(document.body).on('click', 'button.js-save-section', function() {
var parent = $(this).closest(".cars").addClass("saved");
setTimeout(function() {
parent.removeClass("saved");
}, 2E3);
});
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.cars {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
transition: border-color 300ms ease-in-out;
}
.cars.saved {
border-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main">
<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
<br>
<div class="section">
</div>
</div>
<br>
推荐阅读
- c# - 如何使用 dbcontext 使用实体框架核心插入一条或多条记录(soap ui 通过 api 调用将数据发送到 asp.net 核心)
- c++ - 如何使用 ArrayFromJSON 创建日期箭头数组
- r - 闪亮的测验问题
- matlab - 使用 Simscape 操纵器进行计算扭矩控制
- git - git repo 中缺少一行
- c# - ASP.Net HttpPostedFileBase 缺少参考
- javascript - npm 的 docker-compose up 失败
- python - django url 可以在开头包含固定路径吗?
- android - 如何使用绑定适配器从 ImageView 中的 drawable 加载动画 gif?
- java - 无法为元素 Appenders 调用类 org.apache.logging.log4j.core.config.AppendersPlugin 中的工厂方法:java.lang.NullPointerException