>  기사  >  웹 프론트엔드  >  Ajax는 새로 고침 없이 주석 기능을 구현합니다.

Ajax는 새로 고침 없이 주석 기능을 구현합니다.

php中世界最好的语言
php中世界最好的语言원래의
2018-04-24 15:50:312426검색

이번에는 새로 고침 없이 ajax로 댓글 기능을 구현하는 방법을 알려드리겠습니다. ajax로 새로 고침 없이 댓글 기능을 구현하는 경우의 주의사항은 무엇인지 살펴보겠습니다.

이것은 메시지 게시판의 인터페이스입니다. 메시지 제출을 클릭하면 메시지 내용이 비어 있거나 회색 "메시지 내용이 채워지지 않았습니다"라는 메시지가 나타납니다. 메시지 내용을 입력하세요. 사용자가 정보를 입력하면 현재 메시지의 단어 수가 오른쪽 하단에 표시됩니다.

다음은

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); 
  } 
 
});
의 코드입니다. 이 기사의 사례를 읽으신 후 해당 방법을 마스터하셨을 것으로 믿습니다. 더 흥미로운 정보를 보려면 PHP 중국어의 다른 관련 기사를 주목하세요. 웹사이트!

추천 자료:

Ajax는 비동기 로딩을 구현합니다


Ajax 로그인 함수 구현


jQuery+ajax 함수 구현

위 내용은 Ajax는 새로 고침 없이 주석 기능을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.