2011-10-23 143 views
0

我有Magento的不同標籤創建以下下拉的3倍:更改下拉(選擇)用JavaScript是沒有價值的選項

<label>Choose title:</label> 

<select onchange="opConfig.reloadPrice()" title="" class=" required-entry product-custom-option" id="select_13" name="options[13]"> 

<option value="">-- please select --</option> 
<option price="10" value="55">title1 +10,00</option> 
<option price="20" value="56">title2 +20,00</option> 
<option price="30" value="57">title3 +30,00</option> 

</select> 

所有值條目(例如值=「56」),是automaticaly創建,所以我不能選擇應顯示哪些數值。

我必須在JavaScript中選擇一個選項,如「title2 +20,00」,但不使用「value」選項。

這可能嗎?如果是,在哪個方向?

感謝您的每一個有用的答案。

回答

1
function selectOption(sSelectId, sSelectedOptionText) { 
    var oOptions = document.getElementById(sSelectId).getElementsByTagName('OPTION'); 
    for (var i = 0; i < oOptions.length; i++) { 
     if (oOptions[i].innerHTML == sSelectedOptionText) { 
      oOptions[i].selected = true; 
      break; 
     } 
    } 
} 

selectOption('select_13', 'title2 +20,00'); 
selectOption('select_14', 'text 2'); 
selectOption('select_15', 'text 3'); 
... 

也看到我jsfiddle

+0

謝謝!但我也有不同的ID,如「select_13」。我怎樣才能執行你的片段到頁面上的每個選擇框?所有條目都是獨一無二的。謝謝。 – Cellophane

+0

滿足更新的答案? – scessor

+0

差不多。我的意思是這個腳本在整個頁面上搜索