最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于jquery實(shí)現(xiàn)ajax無(wú)刷新評(píng)論

 更新時(shí)間:2020年08月19日 11:49:35   作者:net小伙  
這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)ajax無(wú)刷新評(píng)論的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考

jquery實(shí)現(xiàn)ajax無(wú)刷新評(píng)論需要用的技術(shù):(本次試驗(yàn)用的是“jquery-1.4.2.js”版本的jquery)

$.post("一般處理程序路徑",{以字典的形式傳遞參數(shù)},function(data,status){``````});
jquery中的基本選擇器操作;

首先創(chuàng)建數(shù)據(jù)庫(kù)“T_article”:

主鍵設(shè)置自增;

然后創(chuàng)建一個(gè)強(qiáng)類型的DataSet。

接著創(chuàng)建一個(gè)“無(wú)刷新評(píng)論.aspx”頁(yè)面:

頁(yè)面代碼如下:

 <div>
  <h2>文章:</h2>
  <p>this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!
  this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!
  this a text!this a text!this a text!this a text!this a text!this a text!</p>
  <ul id="pinglunlist">
  </ul>
 </div>
 <textarea id="msg" style="margin-left:20px;" cols="40" rows="10"></textarea>
 <input id="btnpinglun" type="button"
  value="評(píng)論" />

然后創(chuàng)建兩個(gè)一般處理程序WSXPL.ashx(用來(lái)插入數(shù)據(jù)的處理程序)和WSXPL1.ashx(用來(lái)獲取所有評(píng)論數(shù)據(jù)的處理程序);

WSXPL.ashx中的代碼如下:

  public void ProcessRequest(HttpContext context)
  {
   context.Response.ContentType = "text/plain";
   string msg = context.Request["msg"];
   new T_articleTableAdapter().Insert(context.Request.UserHostAddress, msg, DateTime.Now); //創(chuàng)建一個(gè)強(qiáng)類型的實(shí)例,然后調(diào)用Insert()函數(shù)插入;
   context.Response.Write("ok");
  }

WSXPL1.ashx中的代碼如下:

  public void ProcessRequest(HttpContext context)
  {
   context.Response.ContentType = "text/plain";
   var datas = new T_articleTableAdapter().GetData(); //返回的是一個(gè)DataTable
   StringBuilder sb = new StringBuilder(); //創(chuàng)建StringBuilder更加方便的搜集數(shù)據(jù)
   foreach (var data in datas)  //用foreach方法遍歷DataTable
   {//實(shí)現(xiàn)字符串的拼接;每行數(shù)據(jù)用$隔開(kāi),每行數(shù)據(jù)的每個(gè)元素用|隔開(kāi);有利于前臺(tái)解析數(shù)據(jù);
    sb.Append(data.ipaddress).Append("|").Append(data.msg).Append("|").Append(data.posttime).Append("$");
   }
   context.Response.Write(sb);
  }

做完這些步驟,操作數(shù)據(jù)庫(kù)的部分就已經(jīng)完成了?,F(xiàn)在只要在前臺(tái)把一般處理程序返回的數(shù)據(jù)解析一下并附加的相應(yīng)的位置就可以了!

首先在前臺(tái)引用“jquery-1.4.2.js”jquery庫(kù);然后開(kāi)始編寫js腳本;

$(function () {
   $.post("WSXPL1.ashx", function (data, status) { //通過(guò)WSXPL1.ashx獲取所有的評(píng)論內(nèi)容
    if (status == "success") {
     var result = data.split("$"); //按照$分割字符串
     for (var i = 0; i < result.length - 1; i++) {
      var msg = result[i];
      var line = msg.split("|");  //按照|分割字符串
      var pinglun = $("<li>用戶ID:" + line[0] + "; 評(píng)論內(nèi)容:" + line[1] + "; 評(píng)論時(shí)間:" + line[2] + "</li>");
      $("#pinglunlist").append(pinglun); //把得到的評(píng)論結(jié)果追加到ul元素上
     }
    }
    else {
     alert("ajax錯(cuò)誤!");
    }
   })

   $("#btnpinglun").click(function () { //設(shè)置btn事件
    var msg = $("#msg").val();
    $.post("ashx/WSXPL.ashx", { "msg": msg }, function (data, status) {
     if (status == "success") {
      if (data == "ok") {
       $.post("WSXPL1.ashx", function (data, status) { //為了實(shí)現(xiàn)評(píng)論的時(shí)候評(píng)論內(nèi)容會(huì)自動(dòng)的添加到ul上
        if (status == "success") {
         var result = data.split("$");
         var msg = result[result.length - 2];  //獲取最后一條評(píng)論
         var line = msg.split("|");
         var pinglun = $("<li>用戶ID:" + line[0] + "; 評(píng)論內(nèi)容:" + line[1] + "; 評(píng)論時(shí)間:" + line[2] + "</li>");
         $("#pinglunlist").append(pinglun);  //把最后一條評(píng)論追加到ul上
        }
        else {
         alert("ajax錯(cuò)誤!");
        }
       })
       alert("評(píng)論成功!");
      }
      else {
       alert("評(píng)論失??!");
      }
     }
    })

   })
  })

做完這些直接運(yùn)行就可以了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

镇宁| 张家港市| 乌恰县| 琼结县| 英吉沙县| 天峨县| 长垣县| 桐梓县| 平武县| 罗田县| 将乐县| 彰武县| 科技| 崇明县| 图们市| 金门县| 吐鲁番市| 堆龙德庆县| 四川省| 乐陵市| 永丰县| 邳州市| 共和县| 德化县| 巴塘县| 溧水县| 长寿区| 婺源县| 喀什市| 赣榆县| 郴州市| 淳化县| 怀集县| 阿瓦提县| 清流县| 武隆县| 康平县| 泸州市| 偏关县| 泰来县| 昌邑市|