一、Jquery獲取已經選中的checkbox的幾種方式
1.先看看原生js怎么獲取的,首先獲取name為某一對象的checkbox對象,此時接收到的是一個數組,里面存儲的是所有的數組,再對所有數組進行遍歷,如果當前為點擊的對象,就添加就定義的空數組中。
var objetct = document.getElementsByName(“su_check”); var check_list = []; for (k in object) { if (object[k].checked) check_list.push(object[k].value); }
2. 該方法對ie瀏覽器的兼容不太友好,建議將for循環語句更換成如下:for (var i = 0; i < object.length; i++)
3. jquery獲取已經點擊的checkbox復選框
<body>
<input type="checkbox" id="" name="abc" value="123">
<input type="checkbox" id="" name="abc" value="456">
<input type="checkbox" id="" name="abc" value="789">
<button id="btn">點我</button>
</body>
</html>
<script src="jquery.min.js"></script>
<script>
$(function(){
$("#btn").click(function(){
var arr=[];
$("input[name='abc']:checked").each(function(){
arr.push($(this).val());
})
// console.log(arr); //在控制台輸出
for(i in arr){
alert(arr[i]); //彈出復選框中的值
}
})
})
</script>
二、js獲取input輸入框中的值
<body> <input type="text" id="text"> <button id="btn">提交</button> </body> <script> document.getElementById("btn").onclick=function(){ var text = document.getElementById("text").value; alert(text); } </script>