javascript
  • jquery
  • 2016-02-05 79 views 0 likes 
    0

    如何獲取打印在console.log中的值,並將它們設置爲輸入字段中的值?從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); 
    
        }); 
    
    +0

    爲什麼不創建一個函數,除了在bro wser控制檯,更新輸入字段的值? –

    +0

    我想弄清楚如何做到這一點 – ChriChri

    回答

    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); 
        }); 
    
    +0

    給我「對象對象」 – ChriChri

    +0

    @ChriChri,看我的編輯...添加了'JSON.stringify'調用 –

    相關問題