例如:<option value="1" class="1">1111</option>
<option value="2" class="2">1111</option>
<option value="3" class="3">1111</option>
<option value="4" class="4">1111</option>
我怎么在js的function中获取当前选中的哪个class的值

解决方案 »

  1.   

    jquery代码:$('option:selected').attr('class');
      

  2.   


    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>test-selectedclass</title>
    </head>
    <body>
    <select name="" id="test">
    <option value="1" class="1">1111</option>
    <option value="2" class="2">2222</option>
    <option value="3" class="3">3333</option>
    <option value="4" class="4">4444</option>
    </select>
    <script type="text/javascript">
    var test = document.getElementById('test');
    test.onchange = function(){
    for(var i = 0; i < test.options.length; i++){
    var opt = test.options[i];
    if (opt.selected) {
    console.log(opt.className);
    };
    }
    }
    </script>
    </body>
    </html>
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function show(){
    var se=document.getElementById("select");
            var a=se[se.selectedIndex].className;
    alert(a);
    }
    </script>
    </head><body>
    <select id="select" onchange="show()">
    <option value="1" class="1">1111</option>
    <option value="2" class="2">1111</option>
    <option value="3" class="3">1111</option>
    <option value="4" class="4">1111</option>
    </select>
    </body>
    </html>
    这样试试
      

  4.   


    顶一个,忘了有 selectedIndex 活呢。