我试图将我的序列化表单数据发送到 django,它应该作为列表“[视频,音频]”存储在“mylist”中。无论我在表单上检查什么框,views.py 每次都返回 0。
html表单
<form id="myform" method="POST">
<input type="checkbox" name="list" value="audio"/> Audio<br />
<input type="checkbox" name="list" value="video"/> Video<br />
<input type="submit" value="Get Custom Library!" />
</form>
jQuery ajax
<script language="JavaScript">
$(document).ready(function() {
$("#myform").submit(function() {
$.ajax({
type: "POST",
url: 'django/builder/buildit',
data: $(this).serialize(),
success: function(response){
alert(response);
}
});
return false;
});
});
</script>
视图.py
from django.http import HttpResponse
def main(request):
mylist = request.POST.getlist('list')
message = 0
for item in mylist:
if item == 'video':
message = 'vid'
elif item == 'audio':
message = 'aud'
return HttpResponse(message)