1

ストラットのタグを使用してアイテムのリストを表示しています。

<s:select name="example" id="example" list="exampleList" listKey="exampleKey" 
listValue="exampleValue" onchange="fun()"/>

今、私はjavascript関数を持っています:

function fun()
{
  var ex=document.getElementById("example");
  alert(ex.value);
}

この関数では、選択した項目の listValue を取得する必要がありますが、上記のコードを使用すると、選択した listKey を警告するだけです。javascript関数でlistKeyの代わりにlistValueを取得するにはどうすればよいですか?

4

3 に答える 3

2
  $(document).ready(function() {
        $('#reminderTypeID').change(function() 
       {
             var ex = document.getElementById("reminderTypeID");    
        var selTex = ex[$('#reminderTypeID').val()].text;
        var selVal = ex[$('#reminderTypeID').val()].value;
        alert(ex[$('#reminderTypeID').val()].text);  
      });
   });
于 2012-10-19T08:44:47.067 に答える
1

これを試してください(私のために働いた):

function fun()
{
    var ex = document.getElementById("example");
    alert(ex.getAttribute('listValue'));
}

編集
フィドルを追加しました:http://jsfiddle.net/FranWahl/Ur6jT/2/
(なぜそれがうまくいかないのかわかりません.フィドルではうまくいきます.)

于 2012-04-28T01:24:03.503 に答える
0

私はこの同じ問題に遭遇しましたが、他の答えはどちらもうまくいきませんでした。これは私が見つけた解決策です:

    $(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>

于 2014-11-19T15:54:48.230 に答える