js jquery中判斷checkbox是否被選中的方法
在js中:
document.getElementById("checkboxID").checked 返回true或者false
jQuery中:
$("input[type='checkbox']").is(':checked') 返回true或false
1、attr()方法 設置或者返回備選元素的值
attr(屬性名) //獲取屬性的值
attr(屬性名,屬性值) //設置屬性的值
$("#id]").attr("checked") JQ1.6之后返回checked或者是undefined (1.6之錢返回true或者是false)
$("input[type='checkbox']").attr("chacked",true); 將多選框設為全選中狀態 false為不選中狀態
2、prop()方法
$("input[type='checkbox']").prop("checked") 返回true或者false
還有removeAttr(屬性名)、removeProp(屬性名)刪除該屬性
例:
$("input['tupe=checkbox']").removeAttr("checked");移除多選框的選中
================js和jq判斷select是否選中、獲取select選中的值=========
js和jq判斷select是否選中、獲取select選中的值
js和jq獲取select選中的值
JS部分
var mySelect = document.getElementById(”testSelect”);//定位id(獲取select)
var index =mySelect.selectedIndex;// 選中索引(選取select中option選中的第幾個)
var text =mySelect.options[index].text; // 選中文本
var value =mySelect.options[index].value; // 選中值
mySelect.options[index].selected // 判斷select中的某個option是否選中 true為選中 false 為未選中
[html] view plain copy
if(mySelect.options[1].selected == true){
console.log(1)
}
JQ部分
1.判斷option是否被選中
$("#id").is(":checked")//為false時是未被選中的,為true時是被選中
$("#id").attr('checked')==undefined//為false時是未被選中的,為true時是被選中
2.獲取select選中的值
$("#mySelect option:selected").text()
$("#mySelect").find('option:selected').text()
$("#mySelect").val();
3.獲取select選中的索引
$("#mySelect").get(0).selectedindex
4.添加option
$("#mySelect").append("<option value="+value+">"+text+"<option>");
5.刪除option
$("#myOption").remove()
==================================================================
js中獲取方法
var obj = document.getElementByIdx_xx_x(”testSelect”); //定位id
var index = obj.selectedIndex; // 選中索引
var text = obj.options[index].text; // 選中文本
var value = obj.options[index].value; // 選中值
jQuery中獲得選中select值
第一種方式
$('#testSelect option:selected').text();//選中的文本
$('#testSelect option:selected') .val();//選中的值
$("#testSelect ").get(0).selectedIndex;//索引