我试图创建一系列按钮来更新我网站的$_GET['year']变量。我想让这些按钮在这些页面上工作:
- example.com
- example.com/?搜索= foo
- example.com/?搜索= foo&年= 2015。
例如,如果我点击<a>2016</a>
,我的"年份"标签将更新为year=2016
,网页将显示搜索结果或从那一年开始的所有内容。我试过使用JQuery AJAX,像这样:
<ul class="dropdown-menu dateDropdown">
<li><a>2016</a></li>
<li><a>2015</a></li>
<li><a>2014</a></li>
</ul>
<script>
$(document).ready(function () {
$(".dateDropdown>li>a").each(function() {
$(this).click(function() {
alert("pressed");
$.get({
url: "index.php",
data: $(this).html(),
callback: alert("sent")
});
;})
;})
;}
</script>
目前我收到"按下"answers"发送"警报,但网页没有更新。我想我也许可以用窗口让它工作。location和regex来更改或添加'year'到URI,但这种方式会更简洁。什么好主意吗?
看看jQuery。得到参数。
在你的情况下,应该是这样的:
$(".dateDropdown>li>a").each(function(index,elem){
$(elem).click(function(){
alert('Pressed');
$.get('url', {search:'foo', year: $(this).text() }).done( function(){ alert('Done!')} )
})
})
将脚本更改为
<script>
$(document).ready(function () {
$(".dateDropdown>li>a").each(function() {
$(this).click(function() {
alert("pressed");
$year=$(this).html();
$.ajax({
method: 'post',
url: "query.php",
data: 'year='+$year,
callback: alert("sent")
success: function(e){
$('html').html(e);
}
});
;})
;})
;}
</script>
将index.php
的内容拷贝到query.php
使index.php
的内容
<html>
<?php include 'query.php'; ?>
</html>