jQuery技巧

1:自定义函数wyj.utility.js:

$(function () {
    //初始化绑定
    $.extend({
        InitBind: function (obj) {
            obj.empty();
            obj.append($(‘<option/>‘, { value: 0, text: "选择..." }));
        }
    });
    //绑定
    $.extend({
        //obj:$("#ddl")
        //data:JsonResult Json(list, JsonRequestBehavior.AllowGet);
        DdlBind: function (obj, data) {
            $.InitBind(obj);
            $.each(data, function (index, item) {
                //obj.append($(‘<option/>‘, { value: 0, text: "选择..." }));
                //obj.append($(‘<option/>‘).attr(‘value‘, item.Value).text(item.Text));
                obj.append($(‘<option/>‘, { value: item.Value, text: item.Text }));
            });
        }
    });
    //获取数据和绑定
    $.extend({
        //obj:$("#ddl")
        //url:"/Home/GetPersonInfo"
        GetAndDdlBind: function (obj, url) {
            $.ajax({
                url: url,
                type: "GET",
                dataType: "json",
                data: {},
                success: function (data) {
                    DdlBind(obj, data);
                }
            });
        }
    });
});

2:JSON.stringify:

obj2json:

JSON.stringify({ ProductName: "ProductName", SupplierID: "SupplierID", CategoryID: "CategoryID" })

json2obj:

JSON.parseJSON

jQuery.parseJSON

var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
alert(json);

var students = new Array() ;
students[0] = "Lanny";
students[1] = "dong";
students[2] = "I love you";
var json = JSON.stringify(students,switchUpper);
function switchUpper(key, value) {
    return value.toString().toUpperCase();
}
alert(json);

var students = new Array() ;
students[0] = "Lanny";
students[1] = "dong";
students[2] = "I love you";
var stu = new Array();
stu[0] = "1";
stu[1] = "2";
var json = JSON.stringify(students,stu);
alert(json);

var student = new Object();
student.qq = "5485891512";
student.name = "Lanny";
student.age = 25;
var stu = new Array();
stu[0] = "qq";
stu[1] = "age";
stu[2] = "Hi";//这个student对象里不存在。
var json = JSON.stringify(student,stu);
alert(json);

var student = new Object();
student.qq = "5485891512";
student.name = "Lanny";
student.age = 25;
var stu = new Array();
stu[0] = "qq";
stu[1] = "age";
stu[2] = "Hi";
var json = JSON.stringify(student,stu,100);//注意这里的100
alert(json);

var x=[];
x.push({ ProductName: "ProductName", SupplierID: "SupplierID", CategoryID: "CategoryID" });
x.push({ ProductName: "ProductName", SupplierID: "SupplierID", CategoryID: "CategoryID" });
alert(typeof(x));
alert(JSON.stringify(x,null,10));

alert(JSON.stringify({ ProductName: "ProductName", SupplierID: "SupplierID", CategoryID: "CategoryID" }));

3:ajax:

                   var obj = new psersonstatus();
                    $.ajax({
                        type: "POST",
                        url: "../StatusHandler.ashx?type=create",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(obj),//"{‘objString‘:‘" + JSON.stringify("psersonstatus") + "‘}",
                        dataType: ‘json‘,
                        success: function (data) {
                            if (data == "success") {
                                $(‘#dg‘).datagrid("load");
                                top.showTipsMsg("恭喜,信息新增成功!", 4000, 4);
                            }
                            else {
                                top.showTipsMsg("糟糕,信息新增失败!", 4000, 5);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            top.showTipsMsg("糟糕,信息新增失败!", 4000, 5);
                            //alert(textStatus + " " + errorThrown);
                        }
                    });

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。