日期:2014-05-16  浏览次数:20554 次

ajax中添加,删除用户实现无刷新的效果
JScript code
    $(function() {
        getusers();
    })

    function getusers() {
        //通过ajax请求获取数据
        $.ajax({
            url: "getusers.ashx?t=" + new Date(),
            type: "get",
            success: function(msg) {
                //将结果转换为JSON对象
                eval("var js=" + msg);
                var txt = "<table>";
                txt += "<tr><td>用户名</td><td>密码</td><td>性别</td><td>手机</td><td>地址</td><td>操作</td></tr>";
                //解析JSON对象获取数据
                for (var i in js) {
                    txt += "<tr><td>" + js[i].UserName + "</td><td>" + js[i].UserPwd + "</td><td>" + js[i].Sex + "</td><td>" + js[i].Phone + "</td><td>" + js[i].City + "</td><td><a href='javascript:del(" + js[i].ID + ")'>删除</a></td></tr>";
                }
                txt += "</table>";
                $("#divDel").html(txt);
            }
        });
    }

    function add(id) {
        if (confirm("是否确定删除?")) {
            //进行删除操作(通过ajax)
            //使用jquery实现ajax
            $.ajax({
                url: "delUser.ashx?ID=" + id + "&t=" + new Date(),
                type: "get",
                success: function(msg) {
                    if (msg == "0")
                        alert("删除失败!");
                    else {
                        alert("删除成功!");
                        getusers();
                    }
                }
            });
        }
    }



这里我已经写了删除无刷新的效果,下面添加的代码怎么写啊?

------解决方案--------------------
JScript code
<form id="addFrm">
....内容
<input type='button' value='添加' onclick='add()'/>
</form>
<script>
function add(){
 $.ajax({
   url:'xxxxxx.ashx'//写数据库页
   ,type:'POST'
   ,data:$(f).serialize()//////////////
   ,success:function(){/*成功代码*/}
   ,error:function(){/*失败代码*/}
 });
}
</script>

------解决方案--------------------
给你个示例:
function AddInfo() {
var name = document.getElementById("txtName").value;
var pwd = document.getElementById("txtPwd").value;
var sex = "";
var select = document.getElementById("Sex");
for (var i = 0; i < select.length; i++) {
if (select[i].selected) {
sex = select[i].value;
}
}
var phone = document.getElementById("txtPhone").value;
var city = document.getElementById("txtCity").value;
$.ajax({
url: "AddInfo.ashx?",
type: "post",
data:{"name":name,"pwd":pwd,"sex":sex,"phone":phone,"city":city},
success: function(msg) {

if (msg == "0") {
alert("添加失败!");
}
else {
alert("添加成功!");
GetInfo();
}

}
})

}

//AddInfo.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ajax_User
{
 
 
public class AddInfo : IHttpHandler
{

public void P