JQ常用方法(哈哈)
1ajax請求
$(function(){
$("#send").click(function(){
$.ajax({
type:"get",
async:true, //默認設置為true,所有請求均為異步請求。
url: "http://www.idaima.com/xxxxx.php",
data: {
username: $("#username").val(),
content: $("#content").val()
},
dataType: "json", //xml、html、script、jsonp、text
beforeSend:function(){},
complete:function(){},
success: function(data) {
alert(data)
}
error:function(){},
})
})
})
2, 獲取checkbox,判斷是否選中
$("input[type=‘checkbox‘]").is(‘:checked‘)
//返回結果:選中=true,未選中=false
3, 獲取checkbox選中的值
var chk_value =[];
$(‘input[name="test"]:checked‘).each(function(){
chk_value.push($(this).val());
});
each() 方法規定為每個匹配元素規定運行的函數。
語法 $(selector).each(function(index,element))
4,checkbox全選/反選/選擇奇數
$("document").ready(function() {
$("#btn1").click(function() {
$("[name=‘checkbox‘]").attr("checked", ‘true‘); //全選
}) $("#btn2").click(function() {
$("[name=‘checkbox‘]").removeAttr("checked"); //取消全選
}) $("#btn3").click(function() {
$("[name=‘checkbox‘]:even").attr("checked", ‘true‘); //選中所有奇數
}) $("#btn4").click(function() {
$("[name=‘checkbox‘]").each(function() { //反選
if ($(this).attr("checked")) {
$(this).removeAttr("checked");
} else {
$(this).attr("checked", ‘true‘);
}
})
})
})
5,根據索引值設置任意一個radio為選中值:
$(‘input:radio‘).eq(索引值).attr(‘checked‘, ‘true‘);//索引值=0,1,2....
6、jQuery對象與dom對象的轉換
普通的dom對象一般可以通過$()轉換成jquery對象。 $(document.getElementById("msg"))
由於jquery對象本身是一個集合。所以如果jquery對象要轉換為dom對象則必須取出其中的某一項,一般可通過索引取出。
如:$("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5]
JQ常用方法(哈哈)