![](/img/trans.png)
[英]Can not set value dynamically in drop down using javascript/jquery
[英]Unable to set a value in the drop down using javascript
我無法從下拉框中選擇元素。 這是div
<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>
當我運行這個div正確返回
document.getElementById('payflow-form-ccMonth-select-select');
但是,當我嘗試以下操作時,在Chrome上,它沒有在下拉框中設置我想要的值。
document.getElementById('payflow-form-ccMonth-select-select').selectedIndex=1;
我還嘗試過首先使用click()使其聚焦,該方法擴展了下拉框,但仍然無法正常工作。
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;
關於我可能會缺少什么的任何提示? 謝謝
嘗試:
var dropdown = document.getElementById('payflow-form-ccMonth-select-select');
dropdown.options[indexToSelect].selected = true;
我檢查你的代碼和它的工作精細檢查此 。
我能想到的唯一問題是,您的代碼放在了head
並且在實際頁面准備好之前就執行了
你可以嘗試window.onload
window.onload = function(){
document.getElementById('payflow-form-ccMonth-select-select').selectedIndex= 1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.