JQ常用代码

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(){},
        });
    });
});

获取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');

根据Value值设置Radio为选中值

$("input:radio[value='rd2']").attr('checked','true');
$("input[value='rd2']").attr('checked','true');

操作元素的样式

$("#msg").css("background"); //返回元素的背景颜色
$("#msg").css("background","#ccc") //设定元素背景为灰色
$("#msg").height(300); $("#msg").width("200"); //设定宽高
$("#msg").css({ color: "red", background: "blue" });//以名值对的形式设定样式
$("#msg").addClass("select"); //为元素增加名称为select的class
$("#msg").removeClass("select"); //删除元素名称为select的class
$("#msg").toggleClass("select"); //如果存在(不存在)就删除(添加)名称为select的class

jQuery实现回车监听

$(document).keyup(function(event){
  if(event.keyCode ==13){
    $("#submit").trigger("click");
  }
});

 

转载于:https://www.cnblogs.com/daimaxuejia/p/10942664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值