jquery常用技巧与代码总结

时间:2011-08-16 10:41:43 195次阅读 关键词: jquery radio 表格 联动菜单
1.jquery动态删除表格行或单元格
$("a[todo*='del']").click(function(e){
        var id = $(this).attr('todel');//获取ID
        var now=$(this);//防止作用域被篡改
        if (confirm("您确定要删除吗?")) {
            $.get("/p/ajax/pdel.do", {
                id: id
            }, function(data){
                switch (data._rc) {
                    case "success":
                        alert('删除成功', '删除账号');
                        now.parent().parent().find("td").eq(2).html("");
                        now.parent().parent().find("td").eq(3).html("");
                        now.parent().parent().find("td").eq(4).html("");
                        now.parent().parent().find("td").eq(4).html("添加账号");
        //$("#po"+id).remove();删除整行
                        break;
                    default:
                        jAlert("操作成功!");
                        break;
                }
            });
        }
    })
2.jquery实现的省市县级联菜单
$("select[id='studentInfo.provinceId']").change(function(){ //省份下拉菜单的change事件  
        var provId = $(this).val();
        $.ajax({
            url: "/ajax/arealist.do?provId=" + provId, //提交的页面/方法名  
            success: function(data){
                switch (data._rc) {
                    case "success":
                        $("select[id='studentInfo.areaCode'] option").each(function(){
                            $(this).remove(); //移除市区原有项  
                        });
                        $("").appendTo("select[id='studentInfo.areaCode']"); //添加一个默认项  
                        var areaList = data.areaList;
                        $.each(areaList, function(n, value){
                            $("").appendTo("select[id='studentInfo.areaCode']"); //将返回来的项添加到下拉菜单中  
                        });
                        $("select[id='studentInfo.subAreaCode'] option").each(function(){
                            $(this).remove(); //移除县区原有项  
                        });
                        $("").appendTo("select[id='studentInfo.subAreaCode']"); //添加一个默认项  
                        $("select[id='studentInfo.schoolId'] option").each(function(){
                            $(this).remove(); //移除学校原有项  
                        });
                        $("").appendTo("select[id='studentInfo.schoolId']"); //添加一个默认项  
                        break;
                    case "input":
                        jAlert(data._input[0], '获取市区');
                        break;
                    case "error.biz":
                        jAlert(data._error[0], '获取市区');
                        break;
                    default:
                        break;
                }
            }
        });
    });
    $("select[id='studentInfo.areaCode']").change(function(){ //地区下拉菜单的change事件  
        var areaCode = $(this).val();
        $.ajax({
            url: "/ajax/subarealist.do?areaCode=" + areaCode, //提交的页面/方法名  
            success: function(data){
                switch (data._rc) {
                    case "success":
                        $("select[id='studentInfo.subAreaCode'] option").each(function(){
                            $(this).remove(); //移除县区原有项  
                        });
                        $("").appendTo("select[id='studentInfo.subAreaCode']"); //添加一个默认项  
                        var subAreaList = data.subAreaList;
                        $.each(subAreaList, function(n, value){
                            $("").appendTo("select[id='studentInfo.subAreaCode']"); //将返回来的项添加到下拉菜单中  
                        });
                        $("select[id='studentInfo.schoolId'] option").each(function(){
                            $(this).remove(); //移除学校原有项  
                        });
                        $("").appendTo("select[id='studentInfo.schoolId']"); //添加一个默认项  
                        break;
                    case "input":
                        jAlert(data._input[0], '获取县区');
                        break;
                    case "error.biz":
                        jAlert(data._error[0], '获取县区');
                        break;
                    default:
                        break;
                }
            }
        });
    });
3.表格增加与删除
$(function(){
    //表格增加与删除
    var rows = 1;
    $("#add").click(function(){
        ++rows;
        var last_tr = $("#showtable tr:last");
        last_tr.clone(true).insertAfter(last_tr).attr("num", rows).find("a[name='show_del']").attr("num", rows);
        //num保留,作为兼容时的处理
    });
    $("a[name='show_del']").click(function(){
        var rows = $("tr", "#showtable").length;
        if (rows <= 1) {
            alert("必须保留一行!");
        }
        else {
            $(this).parent().parent().remove();
        }
    });
})
4.判断radio选中情况
if($("input:[name=stuIds]:radio:checked").length == 0){
            alert("请选择一个选项");
            return false;
        }


更多