如果将 <a> 放置在 <div id="list"> 之外,我的这段代码效果很好,但当链接放置在目标 div 中时则不行。event.preventDefault() 甚至没有被触发。任何帮助将不胜感激。谢谢!
<script>
$( document ).ready(function() {
$( ".category_link" ).on('click' ,function () {
event.preventDefault();
var addressValue = $(this).attr("href");
function getUrlVars()
{
var vars = [], hash;
var hashes = addressValue.slice(addressValue.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
site_location = getUrlVars()["location"];
category_id = getUrlVars()["category_id"];
per_page = getUrlVars()["per_page"];
request_type = getUrlVars()["request_type"];
//send the request via ajax
$.ajax({
type: "POST",
url: "/test/testing/",
data: {site_location : site_location, category_id : category_id, per_page : per_page},
dataType: "html"
}).done(function( msg ) {
//get the returned message
$( "#list" ).html(msg);
});
});
});
</script>
<div id="list">
<a class="category_link" href="/?site_location=testing&per_page=testing&category_id=testing&request_type=testing">Test</a>