首页 > Web开发 > 详细

jQuery常用方法(持续更新)

时间:2019-11-19 13:06:49      阅读:76      评论:0      收藏:0      [点我收藏+]

(1)AJAX请求

$(function() {
$(‘#send‘).click(function() {
$.ajax({
type: "GET", //GET或POST,
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()); 
});

(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)获取select下拉框的值

直接val就行了

$("#select").val()

(6)获取选中值,三种方法都可以:

$(‘input:radio:checked‘).val();
$("input[type=‘radio‘]:checked").val();
$("input[name=‘rd‘]:checked").val();

 

(7)设置第一个Radio为选中值:

$(‘input:radio:first‘).attr(‘checked‘, ‘checked‘);

或者:

$(‘input:radio:first‘).attr(‘checked‘, ‘true‘);

(8)设置最后一个Radio为选中值:

$(‘input:radio:last‘).attr(‘checked‘, ‘checked‘);

或者:

$(‘input:radio:last‘).attr(‘checked‘, ‘true‘);

 




 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

jQuery常用方法(持续更新)

原文:https://www.cnblogs.com/w-yue/p/11888396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!