0

大家好,我实际上是 jquery 和 javascript 的新手,不过我学到了很多东西。当在文本区域输入“@”时,我实际上是在尝试在墙上的帖子中复制用户的 Facebook 标记。更具体地说,当用户输入“@”时,我有一个文本区域,它将激活我拥有的 ajax 功能。我已经能够列出用户了,我现在只需要 jquery 代码来激活 ajax 功能,当用户在文本区域输入'@'时。我会感谢您的所有帮助:D

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FB TAG</title>
<style>
.namesearch {
    cursor: pointer;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">


function putin_livesearch(fname, lname) 
{

$('#searchbar').val($('#searchbar').val()+fname + " " + lname);

}



function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    document.getElementById("livesearch").style.border="1px solid #A5ACB2";


    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
<form>
  <textarea id ="searchbar" name="wallpost" type="text" rows="3" cols="80"  ></textarea>
  <div id="livesearch"></div>
</form>

</body>

4

2 回答 2

1

像这样?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FB TAG</title>
<style>
.namesearch {
    cursor: pointer;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function putin_livesearch(fname, lname) 
{

$('#searchbar').val($('#searchbar').val()+fname + " " + lname);

}


function showResult(str)
{
    if (str.length==0)
    {
        $("#livesearch").empty().css('border', 0);
    } 
    $.get("getuser.php?q="+str, function(txt) {
        $("#livesearch").html(txt).css('border', "1px solid #A5ACB2");;
    });
}
$(function() {
    $('#searchbar').keyup(function() {
        var val = $(this).val();
        if (val.indexOf('@') !== -1) {
            showResult(val);
        }
    });
});
</script>
</head>

<body>
<form>
  <textarea id ="searchbar" name="wallpost" type="text" rows="3" cols="80"  ></textarea>
  <div id="livesearch"></div>
</form>

</body>
于 2012-09-23T18:06:14.477 回答
0

我能够自己解决它。谢谢“我自己”我爱你

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FB TAG</title>
<style>
.namesearch {
    cursor: pointer;
}
#searchbar {
    width: 458px;
    height: 50px;
    border: solid 2px #333;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    margin-bottom: 6px;
    text-align: left;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://api.jquery.com/scripts/events.js"></script>
</head>

<body>
<form>
  <div id="searchbar" contenteditable="true"></div>
  <div id="livesearch"></div>
</form>
<script type="text/javascript">

function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    document.getElementById("livesearch").style.border="1px solid #A5ACB2";


    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}


var start=/@/ig; // @ Match
var word=/@(\w+)/ig; //@abc Match

$("#searchbar").live("keyup",function() 
{
var content=$(this).text(); //Content Box Data
var go= content.match(start); //Content Matching @
var name= content.match(word); //Content Matching @abc

var searchname = name.toString().substr(1);

if(name.length>0)
{



alert(name);

$("#livesearch").show();

showResult(searchname);



}


return false();

});


function putin_livesearch(fname, lname) 
{

var old=$("#searchbar").html();
var content=old.replace(word," ");
$("#searchbar").html(content);


$('#searchbar').append("<a href ='#'>"+fname+" " +lname+ "</a>");

$("#livesearch").hide();

}



alert('asdasd');


</script>
</body>
</html>
于 2012-09-24T12:30:07.270 回答