Home > Web Front-end > JS Tutorial > Ajax implements comment function without refreshing

Ajax implements comment function without refreshing

php中世界最好的语言
Release: 2018-04-24 15:50:31
Original
2505 people have browsed it

This time I will bring you how to implement the comment function with ajax without refreshing. What are the precautions for implementing the comment function with ajax without refreshing? The following is a practical case, let’s take a look.

This is the interface of the message board. When the user clicks to submit a message, it will be automatically submitted to my message.

The message content is empty, or there is a gray "No message content filled in" A pop-up message will pop up, please fill in the message content. When the user fills in the information, the number of words in the current message will be displayed in the lower right corner.

The following is the code of

javascript

//去掉左右尖括号 并用去掉空格后的字符串替代显示 
function replaceBrackets(id) { 
  var inputValue = $("#" + id).val(); 
  while (inputValue.indexOf("<") != -1) { 
    inputValue = inputValue.replace("<", "["); 
  } 
  while (inputValue.indexOf(">") != -1) { 
    inputValue = inputValue.replace(">", "]"); 
  } 
  while (inputValue.indexOf("&") != -1) { 
    inputValue = inputValue.replace("&", " "); 
  } 
  $("#" + id).val(inputValue); 
} 
 
function replaceChar(name, char) { 
  var inputValue = $("#" + name).val(); 
  while (inputValue.indexOf(char) != -1) { 
    inputValue = inputValue.replace(char, ""); 
  } 
  return inputValue; 
} 
 
$("#txtMessage").blur(function () { 
  $("#txtMessage").val(replaceChar("txtMessage", "<!--")); 
  if ($("#txtMessage").val() == "") { 
    document.getElementById("txtMessage").style.color = "#8C8C8C"; 
    $("#txtMessage").val("没有填写留言内容"); 
    return false; 
  } 
  replaceBrackets("txtMessage"); 
  return true; 
}); 
 
$("#txtMessage").focus(function () { 
  if ($("#txtMessage").val() == "没有填写留言内容") { 
    document.getElementById("txtMessage").style.color = "#000000"; 
    $("#txtMessage").val(""); 
  } 
}); 
 
function txtanum(id, name)  //统计txta的输入字数 
{ 
  var maxl = 151; 
  var num = 150; 
  var content = $("#" + id).val(); 
  content.slice(0, maxl); 
  var nowlength = content.length; 
  if (nowlength >= 0) { 
    if (nowlength < num) 
      $("#" + name).text(nowlength); 
    else 
      $("#" + name).text(num); 
  } else 
    $("#" + id).val(content.substring(0, maxl - 1)); 
 
  if (nowlength == 0) 
    $("#" + name).text(0); 
 
  if (nowlength > num) 
    $("#" + id).val(content.substring(0, num)); 
} 
 
 
var isSubmit = false; 
$('#subMessage').click(function () { 
 
  if (isSubmit) { 
    return; 
  } 
  isSubmit = true; 
  if ($("#txtMessage").val() == "没有填写留言内容" || $.trim($("#txtMessage").val()) == "") { 
    alert("请输入留言内容!"); 
    isSubmit = false; 
    return; 
  } 
  $.ajax({ 
    type: "POST", 
    url: app_param.path_context+"/user/member/msgboard/save", 
    data: { "context": ($("#txtMessage").val()) }, 
    error: function () { 
      isSubmit = false; 
    }, 
    success: function (data) { 
      if (data) { 
        addRow(data); 
        isSubmit=false; 
       $('#zanwu').hide(); 
        document.getElementById("txtMessage").style.color = "#8C8C8C"; 
    $("#txtMessage").val("没有填写留言内容"); 
      }  
    } 
  }); 
  function addRow(messageboard) { 
    var table = $("#tblmsg"); 
    var html = []; 
    html.push("<tr>"); 
    html.push("<td class=&#39;m_time&#39;>"); 
    html.push(messageboard.createDate); 
    html.push("</td>"); 
    html.push("<td>"); 
    html.push(messageboard.context); 
    html.push("</td>"); 
    html.push("<td style=&#39;border-right: 0;&#39; class=&#39;m_order_procz&#39;>"); 
    html.push("<a class=&#39;xxx&#39; href=&#39;messagereply/"+messageboard.id+"&#39;>查看</a>"); 
    html.push("</td>"); 
    html.push("</tr>"); 
    html = html.join(''); 
    table.append(html); 
  } 
 
});
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Other related articles on the Chinese website!

Recommended reading:

Ajax implementation of asynchronous loading

Ajax login function implementation

jQuery ajax function implementation

The above is the detailed content of Ajax implements comment function without refreshing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template