我有两个名为“阅读更多”(代码底部)的链接,其中包含不同的值。单击时,他们调用一个 jquery 函数将其发送到 demo_search.php。问题是,无论我点击哪个链接,我都会得到相同的值。如果我要使用
var searchString = ($(this).attr("href"))
代替
var searchString = ($(".more_info").attr("href"))
一切正常。但我不能使用(this)
. 如何解决这个问题?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Search</title>
<link rel="stylesheet" type="text/css" href="my.css">
<script type="text/javascript" src="http://localhost/dev/ajax/jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".more_info").click(function() {
// getting the value that user typed
var searchString =($(".more_info").attr("href"));
// form the queryString
var data = 'search='+ searchString;
var imgLoad = '<img src="loading.gif">';
// if searchString is not empty
if(searchString) {
alert (searchString);
// ajax call
$.ajax({
type: "GET",
url: "demo_search.php",
data: searchString,
beforeSend: function(html) { // this happens before actual call
$(".word").html(searchString);
$(".imgProgress").html(imgLoad);
},
success: function(page_data){ // this happens after we get results
$("#results").empty();
$("#results").fadeIn(200);
$("#results").append(page_data);
$(".imgProgress").empty();
},
});
}
return false;
});
});
</script>
</head>
<body>
<div id="container">
<div style="margin:20px auto; text-align: center;">
<form method="post">
<input type="text" name="search" id="search_box" class='search_box'/>
<input type="submit" value="Search" class="search_button" /><br />
</form>
</div>
<div>
<div id="searchresults">Search results for <span class="word"></span><span class="imgProgress"></span></div>
<div id="results" class="update" style="display:none;"><ul></ul>
</div>
<div>
<a href="search=sony&offset=20&lang=en" class="more_info">Read More</a>
<a href="search=pioneer&offset=30&lang=en" class="more_info">Read More</a>
</div>
</div>
</div>
</body>
</html>