0

제 옵션에는 몇 가지 색상 입력 값이 있습니다. 미리 설정된 값을로드하고 싶지만 현재는 주어진 값을 선택하고 표시하지 않습니다.자바로 색상 값을 선택하십시오.

html로 :

<h3 class="option">Search Bar Background Color:</h3> 
    <input type="color" class="color searchBarBackground" id="barBackground" name="searchBarBackground"> 

    <h3 class="option">Search Bar Placeholder Text Color:</h3> 
    <input type="color" class="color searchBarPlaceholder" id="placeColor" name="searchBarPlaceholder"> 

    <h3 class="option">Search Bar Text Color:</h3> 
    <input type="color" class="color searchBarText" id="barColor" name="searchBarText"> 

    <h3 class="option">Go Button Background Color:</h3> 
    <input type="color" class="color goButtonBackground" id="goBackground" name="goButtonBackground"> 

자바 스크립트로드 기능 :

window.onLoad = loadOptions(); 

function loadOptions() { 
    var defaultBarBackground = '#000000'; 
    var defaultGoBackground = '#000000'; 
    var defaultBarColor = '#ffffff'; 
    var defaultPlaceColor = '#999999'; 

    chrome.storage.local.get('barBackgroundOption', function (x) { 
     $('#barBackground').attr("value", x.barBackgroundOption); 
    }); 
    chrome.storage.local.get('barColorOption', function (x) { 
     $('#barColor').attr("value", x.barColorOption); 
    }); 
    chrome.storage.local.get('placeColorOption', function (x) { 
     $('#placeColor').attr("value", x.placeColorOption); 
    }); 
    chrome.storage.local.get('goBackgroundOption', function (x) { 
     $('#goBackground').attr("value", x.barBackgroundOption); 

    }); 

나는 문제가 단순히 값을 선택 것을 확신 때문에 나는 페이지 그것에 요소를 검사 할 때 올바른 가치가 있습니다. 단지 표시하지 않습니다.

+1

[jQuery.val()] (http://api.jquery.com/val/)? – MrUpsidown

+0

하하 그게 ... da ** jquery ... 고마워 :) – Nesiehr

+1

기록을 위해, 당신은'get'에 대한 한 번의 호출로 그것을 붕괴해야합니다 : 키 - 디폴트 값 쌍을 가진 객체를 만들어서 그것을 전달하십시오. 'get' 즉,'{defaultBarBackground : '# 000000', /*...*/}' – Xan

답변

1

페이지가로드되기 전에 코드를 호출하고 있습니다. onload에 메소드를 지정하지 않고 리턴 된 것을 지정합니다.

window.onLoad = loadOptions(); 
          ^^ 

window.onLoad = loadOptions; 

또한이 값을 설정 attr()를 사용하고 있는지 이상한 것 같다 할 필요가있다. 그리고 onload를 사용하여로드 할 항목을 설정하지 않아야합니다. 이는 나쁜 습관입니다. 당신이 jQuery를 사용하고 있기 때문에, 당신이 사용하는 것이 jQuery를하지 않고

$(window).on("load", loadOptions); 

를 사용 addEventListener()

관련 문제