获取<select>,<radio>,<checkbox>中未被选中的value值和被选中的value值

<body>中代码

<select id="sel">
    <option value="0">请选择省份:</option>
    <option value="1">云南</option>
    <option value="2">贵州</option>
    <option value="3">四川</option>
</select><br />
<p style="display: inline-block;">爱好:</p>
<input type="checkbox" value="篮球" name="che" />篮球
<input type="checkbox" value="足球" name="che" />足球

一、获取<select>,<checkbox>中未被选中的值

  (1)javascript原生的方法

var sel=document.getElementById("sel");
           for(var i=0;i<sel.length;i++){
               alert(sel[i].value)
           }

  (2)jQuery方法

二、获取<select>,<checkbox>中被选中的value值

  (1)javascript原生的方法    <select>

var sel=document.getElementById("sel");
        sel.onchange=function (){
            var index=sel.selectedIndex;
            alert(sel.options[index].value)
}

  (2)jQuery方法      <select>

$(function(){
        $("select").change(function(){
                alert($("option:selected").val());     
            })
})

猜你喜欢

转载自www.cnblogs.com/xiaohu666/p/10054268.html