从JSON

时间:2017-03-24 08:25:00

标签: javascript jquery json twitter-bootstrap html-datalist

在表格中,我有2个输入。在第一个输入中,我使用一个datalist,我通过JSON加载它,第二个输入在第一个输入改变时自动完成它。

直到这里一切正常!

我添加了一个新按钮,我在bootstrap中使用popover,我想在那里显示一些文本,具体取决于用户的选择(如上所述)。我想要显示的文字是元素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();
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以使用其他forEach循环迭代labels并获取当前选择的versionsquantities,将它们附加到预定义的空变量,然后使用attr()方法将其添加到弹窗框中:

&#13;
&#13;
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();
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果我理解正确,您需要根据所选值更新属性数据内容。

<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>

更新了您的代码段。 可能你需要处理:如何在选择改变时关闭popover

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 data- value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
  
});

$(function() {
  $("#clear").click(function (e) {
    $(".product, .description").val("");
    $('.mytext').popover('hide');
  });

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

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

    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
        labelData = 
          'product ' + a.product + ' | ' +
          'version: ' + a.labels[0].version + 
          ' | quantity: ' + a.labels[0].quantity + " | " +
          'version: ' + a.labels[1].version + 
          ' | quantity: ' + a.labels[1].quantity + "\r\n";
       }
    });
    
    $(this).closest('.form-group').find('.description').val(description);

    $(this).closest('.form-group').find('.mytext').attr("data-content", labelData);
    $('.mytext').popover('toggle').popover('show');

  });
});

// $('.mytext').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" role="form">
  <fieldset>
    <div class="form-group">
      <a class="btn btn-danger" id="clear">clear</a>
    
      <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">
        <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>