2014-02-19 40 views
0

我無法從下拉框中選擇元素。 這裏是div無法使用javascript設置下拉列表中的值

<div class="select ccMonth" id="payflow-form-ccMonth-select"> 
<select name="expMonth" class="month payflow-form-ccMonth-select hidden" id="payflow-form-ccMonth-select-select"> 
<option value="Month">Month</option> 
<option value="1">1</option> 
<option value="2">2</option> 
</select> 
<span name="expirationMonthSpan">Month</span><ul> 
<li data-index="0">Month</li> 
<li data-index="1">1</li> 
<li data-index="2">2</li> 
</ul> 
</div> 

當我在Chrome但是運行此

document.getElementById('payflow-form-ccMonth-select-select'); 

當我嘗試下面的DIV返回正確,它不會在下拉框中設置值,我想。

document.getElementById('payflow-form-ccMonth-select-select').selectedIndex=1; 

我也試圖用點擊()擴大下拉框,但它仍然無法工作,它首先重點。

document.getElementById('payflow-form-ccMonth-select-select').click(); 
document.getElementById('payflow-form-ccMonth-select-select').selectedIndex=1; 

我也試過這個,沒有工作或者

document.getElementById('payflow-form-ccMonth-select-select').value = 1; 

什麼我可能會丟失任何提示? 感謝

+0

經過你的代碼,它的工作檢查http://jsbin.com/zez/2/edit – Champ

回答

0

嘗試:

var dropdown = document.getElementById('payflow-form-ccMonth-select-select'); 
    dropdown.options[indexToSelect].selected = true; 
0

我檢查你的代碼和它的工作精細檢查This

我能想到的唯一的問題是,你的代碼放置在head並得到實際的頁面之前執行的準備

你可以嘗試window.onload

window.onload = function(){ 
    document.getElementById('payflow-form-ccMonth-select-select').selectedIndex= 1; 

} 
相關問題