【发布时间】:2012-02-11 18:44:12
【问题描述】:
我很难弄清楚下面代码中的 load() 函数有什么问题。 这段代码的基本作用是通过一个表单(其 id 为 caltag)从用户那里获取一个日期(在表单中命名为 tabledate),并给出一个包含当天所有事件的表。但无论我通过表格给出的日期,我总是得到今天的日程安排。我不确定即使 jquery 部分正在完全执行,因为 正如代码所说,提交按钮甚至被禁用。此外,我不知道如何调试 js 程序。 已经被困了几个小时。任何建议都会有很大帮助。
$("#caltag").submit(
function(e){
$("#caltag").attr('action', '/table/fetch/');
e.preventDefault();
$("submit").attr('disabled',true);
var calform=$("#caltag");
var values = $("#caltag").serializeArray();
$("#table").load(
$("#caltag").attr('action') + "#table",
values,
function() {
$("#table").hide().fadeIn(1000);
$("#getdate").attr('disabled', false);
});});
查看部分
def table(request): # '/table/fetch/' requests of this view
events=[['_'] * 5 for row in range(5)]
if request.method=='POST':
datestring=request.POST['tabledate']
date=datetime.strptime(datestring,'%m/%d/%Y').strftime('%Y-%m-%d')
else:
date=datetime.today()
eventset=Location.objects.filter(eventdate=date)
for event in eventset:
events[event.x][event.y]=event
return render(request, 'scheduler/table.html',{'events':events,'tabdate':date})
同样的html表单。
<div id="cal" class="span-10">
<form method='POST' id='caltag' action="/table/fetch/">
<p>
<label for="datepicker">tabledate:</label>
<input type="text" id="datepicker" name="tabledate" />
</p>
<input type='submit' id="getdate">
</form>
</div>
【问题讨论】:
-
$("#caltag").attr('action') + "#table" 没有意义,应该只有一个#table。如果您的意思是 .table,那么 attr('action') 和 .table 之间需要有空格,例如“.table”。要调试,您可以使用 alert(variable) 或 console.log + firebug
-
在 chrome 中打开检查器,或者在 firefox 中打开 firebug 并跟踪发送的请求,看看到底什么发送到服务器,什么发回。