从console.log获取值并在输入字段中显示/ print / return

时间:2016-02-05 18:11:30

标签: javascript jquery

如何获取console.log中打印的值,并将它们设置为输入字段中的值?

这是JSfiddle,包含我到目前为止的所有内容 - https://jsfiddle.net/ubo1heru/

将值放在console.log中的代码:

 $("button").click(function() {
    var dishkoMap = {};
    $("select[name='diskho'] > option:selected").each(function() {
      var value = this.value;
      if (dishkoMap[value]) { // if value already exists then increase it by 1
        dishkoMap[value] += 1;
      } else {
        dishkoMap[value] = 1;
      }
    });
    // dishkoMap is a dictionary object so you won't see anything in alert.
    // open the browser console and you can see the counts corresponding
    // to each selected option
    console.log(dishkoMap);

  });

1 个答案:

答案 0 :(得分:1)

只需创建一个function,除了在浏览器控制台中打印外,还会更新input字段的值:

 var printToDishkoMap = function (data) {
    // Stringifying data is it is a JSON
    $('input.diskhoval').val(JSON.stringify(data));
    console.log(data);
 }

 $("button").click(function() {
    var dishkoMap = {};
    $("select[name='diskho'] > option:selected").each(function() {
      var value = this.value;
      if (dishkoMap[value]) { // if value already exists then increase it by 1
        dishkoMap[value] += 1;
      } else {
        dishkoMap[value] = 1;
      }
    });
    // dishkoMap is a dictionary object so you won't see anything in alert.
    // open the browser console and you can see the counts corresponding
    // to each selected option
    printToDishkoMap(dishkoMap);
  });