当我 console.log on bootstrap tagsinput 时发出重复值
Issue duplicates value when I console.log on bootstrap tagsinput
我的 Bootstrap Tagsinput 有一个错误,如下所示,当我点击 'Add property
' 按钮时,我附加了一行 div (在该行内有 tagsinput),然后我单击“Show
”按钮以显示 console.log
。但是我得到一个错误是 console.log
当我有两行或更多行时显示重复值。
这是我的代码笔:https://codepen.io/LinhNT/pen/WNvmygx。
$(document).ready(function () {
$('.js_add_property').click(function () {
const wraper = $('#js_add_property_wraper');
addProperty(wraper);
});
});
function addProperty(wraper) {
const template = `<div class="row property-row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label>Tags</label>
<input type="text" class="tags-input">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Actions</label>
<div>
<button class="btn btn-primary js_show_data">Show</button>
<button class="btn btn-secondary js_remove_row">Remove</button>
</div>
</div>
</div>
</div>`;
wraper.append(template);
$('.tags-input').tagsinput();
showTableProduct();
const button = $('.js_remove_row');
removeRow(button);
}
function removeRow(button) {
button.click(function () {
$(this).parents('.property-row').remove();
$('.tags-input').tagsinput('reset');
$('.tags-input').tagsinput();
})
}
function showTableProduct() {
$('.js_show_data').click(function () {
const tagInput = $(this).parents('.property-row').find('.tags-input');
console.log(tagInput.tagsinput('items'))
})
}
.bootstrap-tagsinput {
display: block;
width: 100%;
height: calc(1.5em + .75rem + 2px);
padding: .375rem .75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: .25rem;
transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.bootstrap-tagsinput input {
padding: 0;
}
.label {
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: 700;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
}
.label-info {
background-color: #5bc0de;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
<div class="display-list">
<div id="js_add_property_wraper"></div>
<button class="btn btn-primary js_add_property">Add property</button>
</div>
谢谢。
尝试像这样修改您的代码:
$(document).ready(function () {
$('.js_add_property').click(function () {
const wraper = $('#js_add_property_wraper');
addProperty(wraper);
});
$(document).on('click','.js_show_data', function(){
const tagInput = $(this).parent().parent().parent().parent('.property-row').find('.tags-input');
console.log(tagInput.tagsinput('items'));
});
});
function addProperty(wraper) {
const template = `<div class="row property-row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label>Tags</label>
<input type="text" class="tags-input">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Actions</label>
<div>
<button class="btn btn-primary js_show_data">Show</button>
<button class="btn btn-secondary js_remove_row">Remove</button>
</div>
</div>
</div>
</div>`;
wraper.append(template);
$('.tags-input').tagsinput();
const button = $('.js_remove_row');
removeRow(button);
}
// showTableProduct();
function removeRow(button) {
button.click(function () {
$(this).parents('.property-row').remove();
$('.tags-input').tagsinput('reset');
$('.tags-input').tagsinput();
})
}
我还附上了屏幕,它也证明在控制台中它只显示点击按钮的输入值
还更新了我的笔
我的 Bootstrap Tagsinput 有一个错误,如下所示,当我点击 'Add property
' 按钮时,我附加了一行 div (在该行内有 tagsinput),然后我单击“Show
”按钮以显示 console.log
。但是我得到一个错误是 console.log
当我有两行或更多行时显示重复值。
这是我的代码笔:https://codepen.io/LinhNT/pen/WNvmygx。
$(document).ready(function () {
$('.js_add_property').click(function () {
const wraper = $('#js_add_property_wraper');
addProperty(wraper);
});
});
function addProperty(wraper) {
const template = `<div class="row property-row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label>Tags</label>
<input type="text" class="tags-input">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Actions</label>
<div>
<button class="btn btn-primary js_show_data">Show</button>
<button class="btn btn-secondary js_remove_row">Remove</button>
</div>
</div>
</div>
</div>`;
wraper.append(template);
$('.tags-input').tagsinput();
showTableProduct();
const button = $('.js_remove_row');
removeRow(button);
}
function removeRow(button) {
button.click(function () {
$(this).parents('.property-row').remove();
$('.tags-input').tagsinput('reset');
$('.tags-input').tagsinput();
})
}
function showTableProduct() {
$('.js_show_data').click(function () {
const tagInput = $(this).parents('.property-row').find('.tags-input');
console.log(tagInput.tagsinput('items'))
})
}
.bootstrap-tagsinput {
display: block;
width: 100%;
height: calc(1.5em + .75rem + 2px);
padding: .375rem .75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: .25rem;
transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.bootstrap-tagsinput input {
padding: 0;
}
.label {
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: 700;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
}
.label-info {
background-color: #5bc0de;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
<div class="display-list">
<div id="js_add_property_wraper"></div>
<button class="btn btn-primary js_add_property">Add property</button>
</div>
谢谢。
尝试像这样修改您的代码:
$(document).ready(function () {
$('.js_add_property').click(function () {
const wraper = $('#js_add_property_wraper');
addProperty(wraper);
});
$(document).on('click','.js_show_data', function(){
const tagInput = $(this).parent().parent().parent().parent('.property-row').find('.tags-input');
console.log(tagInput.tagsinput('items'));
});
});
function addProperty(wraper) {
const template = `<div class="row property-row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label>Tags</label>
<input type="text" class="tags-input">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Actions</label>
<div>
<button class="btn btn-primary js_show_data">Show</button>
<button class="btn btn-secondary js_remove_row">Remove</button>
</div>
</div>
</div>
</div>`;
wraper.append(template);
$('.tags-input').tagsinput();
const button = $('.js_remove_row');
removeRow(button);
}
// showTableProduct();
function removeRow(button) {
button.click(function () {
$(this).parents('.property-row').remove();
$('.tags-input').tagsinput('reset');
$('.tags-input').tagsinput();
})
}
我还附上了屏幕,它也证明在控制台中它只显示点击按钮的输入值
还更新了我的笔