Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

jQuery之ajax post篇

Posted on 2008-06-13 10:06  analyzer  阅读(808)  评论(0)    收藏  举报
http://docs.jquery.com/Plugins#Forms jq form plugin
 1<script type="text/javascript">    
 2function adddata()    
 3    {    
 4      var typeName=$("#<%=this.typeName.ClientID%>").val();    
 5      var msg=" not be empty";    
 6     if(typeName=="")    
 7     {    
 8        if(msg!="")    
 9          {    
10             alert(msg);    
11            return false;    
12           }
     
13      }
    
14     else   
15     {    
16         //显示进度条    
17          $("#loading").ajaxStart(function(){    
18          $(this).show();    
19          }
);    
20             
21         //提交前触发的事件    
22          $("#msg").ajaxSend(function(request, settings){$(this).append("<li>Starting request at " + settings.url + "</li>");});    
23   
24          //这里的countryid   可以动态从GridView里面取    
25           var countryid= $("#<%=this.drpCountry.ClientID%>").val();//获取下拉菜单值    
26           var countryname=format_get_name(countryid);//获取下拉菜单文本    
27           var typeName = $("#<%=this.typeName.ClientID%>").val();//获取txt为typeName的值    
28           var showTypeDesc = $("#<%=this.showTypeDesc.ClientID%>").val();//获取txt为showTypeDesc的值    
29               
30           //调用Juqery Ajax    
31            $.ajax({    
32            type: "POST",    
33            url: "addNews.aspx",    
34            timeout: 20000,    
35            error: function(){alert('error');},    
36            data: "countryid="+countryid+"&countryname="+countryname+"&typeName="+typeName+"&showTypeDesc="+showTypeDesc,    
37            success: function(msg)    
38           {    
39               
40            var text=msg.split('<');    
41           //当AJAX请求失败时添加一个被执行的方法    
42            $("#msg").ajaxError(function(request, settings){    
43            $(this).append("<li>Error requesting page " + settings.url + "</li>");    
44            }
);    
45               
46            //当AJAX请求成功时添加一个被执行的方法    
47            $("#msg").ajaxSuccess(function(request, settings){    
48            $(this).append(text[0]);    
49            }
);    
50   
51          //清空文本里面的值    
52            $("#<%=this.typeName.ClientID%>").val("");    
53            $("#<%=this.showTypeDesc.ClientID%>").val("");    
54           return false;    
55            }
    
56            }
);    
57       }
    
58    }
    
59       
60    //获取下拉菜单里面的文本内容    
61     function format_get_name(id)    
62    {    
63         var drp = $('<%=drpCountry.ClientID%>');    
64        for ( var i =0;i<drp.options.length;i++)    
65        {    
66            if ( drp.options[i].value == id )    
67            {    
68                return drp.options[i].text;    
69             }
    
70         }
    
71        return '';    
72     }
    
73</script>   
74