獲取<select>,<radio>,<checkbox>中未被選中的value值和被選中的value值
阿新 • • 發佈:2018-12-02
<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()); }) })