gpt4 book ai didi

javascript - 从 JSON 自动完成 Bootstrap 弹出窗口的内容

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:35 25 4
gpt4 key购买 nike

在一个表单中,我有 2 个输入。在第一个输入中,我使用一个数据列表,通过 JSON 加载它,第二个输入在第一个输入更改时自动完成。

到目前为止一切正常!

我添加了一个新按钮,我使用 Bootstrap 中的弹出窗口,我想根据用户的选择在其中显示一些文本(如上所述)。我要显示的文本是元素labels的信息。

我读了相关的question但到目前为止我还没有成功..

var dataList = $('.products');
var jsonOptions = [{
"product": "11111",
"description": "description 1",
"labels": [{
"version": "01",
"quantity": 500
}, {
"version": "02",
"quantity": 800
}, ]
}, {
"product": "22222",
"description": "description 2",
"labels": [{
"version": "01",
"quantity": 900
}, {
"version": "02",
"quantity": 100
}, ]
}, {
"product": "33333",
"description": "description 3",
"labels": [{
"version": "01",
"quantity": 200
}, {
"version": "02",
"quantity": 4300
}, ]
}];

jsonOptions.forEach(function(item) {

var option = '<option value="' + item.product + '">' + item.description + '</option>';

dataList.append(option);
});

$(function() {
$('body').on('input', '.product,.products', function() {

var i = this.value;
var description = "";
var productsInBox = 0;
var text = "";

jsonOptions.forEach(function(a) {
if (a.product == i) {
description = a.description;
productsInBox = a.productsInBox;
for (let i = 0, l = a.labels.length; i < l; i++) {
text += "version " + a.labels[i].version + " has " + a.labels[i].quantity + "\n"
}

}
});
$(this).closest('.form-group').find('.description').val(description);
$(this).closest('.form-group').find('.mytext').val(text);
console.log(text);

});
});

$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<form id="form1" method="post" class="form-horizontal" role="form">
<fieldset>
<div class="form-group">
<div class="col-sm-2">
<input type="text" list="products" class="form-control product" name="product[]" />
<datalist id="products" class="products"></datalist>
</div>

<div class="col-sm-3">
<input id="" type="text" class="form-control description" name=" description[]" />
</div>


<div class="col-sm-3" style="margin-left: 10px;">
<button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like: version 01 has 500 "><i class="fa fa-info"></i></button>
</div>

</div>
</fieldset>
</form>

最佳答案

您可以使用另一个forEach循环来迭代标签并获取版本数量当前选择,将它们附加到预定义的空变量,然后使用 attr() 方法将它们添加到弹出窗口:

var dataList = $('.products');
var jsonOptions = [{
"product": "11111",
"description": "description 1",
"labels": [{
"version": "01",
"quantity": 500
}, {
"version": "02",
"quantity": 800
}, ]
}, {
"product": "22222",
"description": "description 2",
"labels": [{
"version": "01",
"quantity": 900
}, {
"version": "02",
"quantity": 100
}, ]
}, {
"product": "33333",
"description": "description 3",
"labels": [{
"version": "01",
"quantity": 200
}, {
"version": "02",
"quantity": 4300
}, ]
}];

jsonOptions.forEach(function(item) {

var option = '<option value="' + item.product + '">' + item.description + '</option>';

dataList.append(option);
});

$(function() {
$('body').on('input', '.product,.products', function() {

var i = this.value;
var description = "";
var productsInBox = 0;
var text = "";
var version = '';
var quantity = '';
jsonOptions.forEach(function(a) {
if (a.product == i) {
description = a.description;
productsInBox = a.productsInBox;
text = a.labels
a.labels.forEach(function(el) {
version += el.version + " ";
quantity += el.quantity + " ";
});
}
});

$('[data-toggle="popover"]').attr('data-content', "versions are: " + version + ' and quantity are: ' + quantity).data('bs.popover').setContent();
$(this).closest('.form-group').find('.description').val(description);
$(this).closest('.form-group').find('.mytext').val(description);


});
});

$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<form id="form1" method="post" class="form-horizontal" role="form">
<fieldset>
<div class="form-group">
<div class="col-sm-2">
<input type="text" list="products" class="form-control product" name="product[]" />
<datalist id="products" class="products"></datalist>
</div>

<div class="col-sm-3">
<input id="" type="text" class="form-control description" name=" description[]" />
</div>

<div class="col-sm-3" style="margin-left: 10px;">
<button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like: version 01 has 500 "><i class="fa fa-info"></i></button>
</div>

</div>
</fieldset>
</form>

关于javascript - 从 JSON 自动完成 Bootstrap 弹出窗口的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42994630/

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