私はこの同じ問題に遭遇しましたが、他の答えはどちらもうまくいきませんでした。これは私が見つけた解決策です:
$(document).ready(function(){
$("#example").change(function(){
var options = document.getElementById("example").options;
var selectedIndex = document.getElementById("example").selectedIndex;
var selectedOptionText = options[selectedIndex].text;
var selectedOptionValue = options[selectedIndex].value;
console.log("Id of selected option is: " + selectedOptionValue);
console.log("Text of selected option is: " + selectedOptionText);
});
});
この問題をさらに理解するために、JSP が処理されるときにどのような HTML が作成されるかを考えることが役に立ちました。<s:select>
は次の HTML を生成します。
<select id="example" name="example">
<option value="456">TextA</option>
...
</select>
OPのサンプルコードに従って、「456」はから派生しexampleKey
、「TextA」はから派生しexampleValue
ます。<option>
の各アイテムには、そのような HTML がありますexampleList
。
私のソリューションでは、jQuery.change()
関数を使用したことに注意してください。onchange="fun()"
ただし、OPが行ったように、私も解決策を試しましたが、それもうまくいきました。
に置き換えdocument.getElementById("example")
てみたところ$("#example")
、解決策がうまくいきませんでした。
オプション selectedValueに関するこのリファレンスを読むことも役に立ちました。<select>