Asp.net使用Jquery编辑和保存的相同按钮

时间:2015-09-04 04:38:43

标签: javascript jquery asp.net

我有类似的东西, A UI Look

当我点击编辑时,它会将该学生的记录填充回文本框。我想使用相同的' Save'按钮保存和编辑。我也通过相同的按钮保存学生。 我希望当有人点击编辑按钮时保存ajax呼叫不会调用。我被困在那。目前,当我编辑记录时,也插入了相同的记录。如果有人想看到代码,我可以为此编辑问题。 感谢

function UpdateStudent(id, name, fname, roll, age, phone, address) {
debugger
$(document).ready(function () {
    $("#students").show();
    $("#txtName").val(name);
    $("#txtFatherName").val(fname);
    $("#txtRollNo").val(roll);
    $("#txtAge").val(age);
    $("#txtPhone").val(phone);
    $("#txtAddress").val(address);
    if (id) {
        $("#btnSave").click(function (e) {
            e.preventDefault();
            debugger
            var Name = $("#txtName").val();
            var FatherName = $("#txtFatherName").val();
            var RollNo = $("#txtRollNo").val();
            var Age = $("#txtAge").val();
            var Phone = $("#txtPhone").val();
            var Address = $("#txtAddress").val();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "StudentManagement.aspx/UpdateStudent",
                data: "{'ID': '" + id + "','Name':'" + Name + "','FatherName':'" + FatherName + "','RollNo':'" + RollNo + "','Age':'" + Age + "','Phone':'" + Phone + "','Address':'" + Address + "'}",
                dataType: "json",
                success: function (data) {
                    debugger
                    $("#txtName").val("");
                    $("#txtFatherName").val("");
                    $("#txtRollNo").val("");
                    $("#txtAge").val("");
                    $("#txtPhone").val("");
                    $("#txtAddress").val("");
                    $("#students").hide();
                    var array = data.d;
                    $("#table").find("tr:gt(0)").remove();
                    for (var i = 0; i < array.length - 1; i++) {
                        var row = "<tr>"
                        + "<td>" + array[i].ID + "</td>"
                        + "<td>" + array[i].Name + "</td>"
                        + "<td>" + array[i].FatherName + "</td>"
                        + "<td>" + array[i].RollNo + "</td>"
                        + "<td>" + array[i].Age + "</td>"
                        + "<td>" + array[i].Phone + "</td>"
                        + "<td>" + array[i].Address + "</td>"
                        + "<td><a href='#' onclick='UpdateStudent(\"" + array[i].ID + "\",\"" + array[i].Name + "\",\"" + array[i].FatherName + "\",\"" + array[i].RollNo + "\",\"" + array[i].Age + "\",\"" + array[i].Phone + "\",\"" + array[i].Address + "\")'>Edit</a></td>"
                        + "<td><a href='#' onclick='DeleteStudent( " + array[i].ID + " )'>Delete</a></td>"
                        + "</tr>"
                        $("#table").append(row);
                    }
                },
                error: function (response) {
                    debugger
                    alert(response);
                }
            });
            return false;
        });
    }
})

}

并在下面插入学生代码,也是

function InsetStudent() {
debugger
$(document).ready(function () {
    var Name = $("#txtName").val();
    var FatherName = $("#txtFatherName").val();
    var RollNo = $("#txtRollNo").val();
    var Age = $("#txtAge").val();
    var Phone = $("#txtPhone").val();
    var Address = $("#txtAddress").val();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "StudentManagement.aspx/CreateStudent",
        data: "{'Name':'" + Name + "','FatherName':'" + FatherName + "','RollNo':'" + RollNo + "','Age':'" + Age + "','Phone':'" + Phone + "','Address':'" + Address + "'}",
        dataType: "json",
        success: function (data) {
            debugger
            $("#txtName").val("");
            $("#txtFatherName").val("");
            $("#txtRollNo").val("");
            $("#txtAge").val("");
            $("#txtPhone").val("");
            $("#txtAddress").val("");
            $("#students").hide();
            var array = data.d;
            $("#table").find("tr:gt(0)").remove();
            for (var i = 0; i < array.length; i++) {
                var row = "<tr>"
                + "<td>" + array[i].ID + "</td>"
                + "<td>" + array[i].Name + "</td>"
                + "<td>" + array[i].FatherName + "</td>"
                + "<td>" + array[i].RollNo + "</td>"
                + "<td>" + array[i].Age + "</td>"
                + "<td>" + array[i].Phone + "</td>"
                + "<td>" + array[i].Address + "</td>"
                + "<td><a href='#' onclick='UpdateStudent(\"" + array[i].ID + "\",\"" + array[i].Name + "\",\"" + array[i].FatherName + "\",\"" + array[i].RollNo + "\",\"" + array[i].Age + "\",\"" + array[i].Phone + "\",\"" + array[i].Address + "\")'>Edit</a></td>"
                + "<td><a href='#' onclick='DeleteStudent( " + array[i].ID + " )'>Delete</a></td>"
                + "</tr>"
                $("#table").append(row);
            }
        },
        error: function (response) {
            debugger
            alert(response);
        }
    });
    return false;
})

}

insertStudent调用jquery load,如

$("#btnSave").click(function (e) {
    e.preventDefault();
    InsetStudent();
})

2 个答案:

答案 0 :(得分:1)

仅删除btnSave click个事件

$("#btnSave").click(function (e) {
});

来自function UpdateStudent并致电

直接在function UpdateStudent点击事件

edit button

答案 1 :(得分:1)

您可以这样做:

<强> STEP1:

如果表单中有任何必填字段,请使用此

$("#btnSave").click(function (e) {
        e.preventDefault();
    if(Check value of required field not null or empty)
    {
       UpdateStudent(id, name, fname, roll, age, phone, address);
    }
    else
    {
          InsetStudent();
    }
});

<强> STEP2:

如果您未在表单中使用任何必填字段,请使用隐藏字段。此隐藏字段的默认值为false。当用户点击编辑时,首先将true分配给隐藏字段。

 $("#btnSave").click(function (e) {
        e.preventDefault();
    if(Check value of Hidden field true)
    {
       UpdateStudent(id, name, fname, roll, age, phone, address);
  //Here update hidden field value to false after completing the operation
    }
    else
    {
          InsetStudent();
    }
});

希望这个逻辑可以帮到你。