jQuery常用方法

  • 如何获取checkbox,并判断是否选中
$("input[type='checkbox']").is(':checked') 
//返回结果:选中=true,未选中=false在这里插入代码片
  • 获取checkbox选中的值
var chk_value =[]; 
$('input[name="test"]:checked').each(function(){ 
	chk_value.push($(this).val()); 
});
  • 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');
			}
		})
	})
})
  • 获取select下拉框的值
$("#select").val()
  • 获取选中值,三种方法都可以:
$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();
  • 设置第一个Radio为选中值:
$('input:radio:first').attr('checked', 'checked');

$('input:radio:first').attr('checked', 'true');
  • 设置最后一个Radio为选中值:
$('input:radio:last').attr('checked', 'checked');

$('input:radio:last').attr('checked', 'true');
  • 根据索引值设置任意一个radio为选中值:
$('input:radio').eq(索引值).attr('checked', 'true');//索引值=0,1,2....

$('input:radio').slice(1,2).attr('checked', 'true');
  • 根据Value值设置Radio为选中值
$("input[value='rd2']").attr('checked','true');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值