[英]How to save changed state of dynamic created checkbox
I have a problem with saving state of checkbox which is created in modal dialog via jquery function append()
.我在保存通过 jquery 函数append()
在模态对话框中创建的复选框状态时遇到问题。 The problem is when you close modal and open it again that state it's rewrite with append()
function.问题是当您关闭模态并再次打开它时,它会使用append()
函数重写该状态。
The problem is that on every modal call you're rebuilding it's .html()
with a brand new HTML string.问题在于,在每个模态调用中,您都使用全新的 HTML 字符串重建它的.html()
。
Instead create an in-memory checkbox element and append that one.而是创建一个内存中的复选框元素并附加该元素。
var $cbx = $("<input>", {
type: "checkbox",
value: "",
class: "form-control checkboxpicker",
checked: true,
name: ""
});
$('button[data-target="#myModal"]').on('click', function(){
$('#app').html('<div class="checkboxContainer">Checkbox: </div>');
$('.checkboxContainer').append($cbx); // Append our in-memory checkbox!
$cbx.checkboxpicker();
});
To recap, whatever happens to the $cbx
checked
state, will always be remembered internally inside the $cbx
.回顾一下,无论$cbx
checked
状态发生了什么,都将始终在$cbx
内部被记住。 Re-appending that item - you'll always get it's last ( current ) state.重新附加该项目 - 您将始终获得它的最后(当前)状态。
Use $('.checkboxpicker').prop('checked',active);
使用$('.checkboxpicker').prop('checked',active);
and initialize active
as false/true
initially.并最初将active
初始化为false/true
。
var active = false; $('button[data-target="#myModal"]').on('click', function() { $('#app').html('<div class="checkboxContainer">Checkbox: </div>'); $('.checkboxContainer').append( '<input class="checkboxpicker" name="" value="" type="checkbox" class="form-control">' ); $('.checkboxpicker').prop('checked', active); $('.checkboxpicker').checkboxpicker(false); }); $(document).on('change', '.checkboxpicker', function() { active = this.checked; console.log(active); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-checkbox/1.4.0/bootstrap-checkbox.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet" /> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <!-- Button trigger modal --> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> </button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> <div id="app"> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.