我正在尝试在Google地图上移动表单的提交按钮。我可以移动按钮,但是在我用javascript重新定位后它没有提交。这是我的代码:
模板
<form method="POST" class="post-form" id="thing">
{{ form.as_p }}
<button id="saving" type="submit" class="save btn btn-default controls">Let's Go!</button>
<input id="pac-input" class="controls" type="text" placeholder="Where do you want to go?">
{% csrf_token %}
</form>
<div id="map"></div>
模板中的javascript
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
var homeControlDiv = document.getElementById('saving');
map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(homeControlDiv);
views.py
def search(request):
form = LocationInput()
if request.method == "POST":
print '1'
form = LocationInput(request.POST)
if form.is_valid():
print '2'
t = Trip(trip_name = form.cleaned_data['city'])
t.save()
if Location.objects.filter(google_id=form.cleaned_data['google_id']).exists():
l = Location.objects.get(google_id=form.cleaned_data['google_id'])
l.trips.add(t)
l.save()
else:
form.save()
l = Location.objects.get(google_id=form.cleaned_data['google_id'])
l.trips.add(t)
l.save()
if request.user.is_authenticated():
t.owner = request.user
t.save()
return HttpResponseRedirect('/trips/')
else:
request.session['trip-id'] = t.id
return HttpResponseRedirect('/auth/')
return render(request, 'frontpage/search.html', {'form': form})
forms.py
class LocationInput(forms.ModelForm):
class Meta:
model = Location
widgets = {
'name' : forms.TextInput(attrs = {'placeholder': 'I want to go to...', 'size': '15', 'type': 'hidden'}),
'google_id' : forms.TextInput(attrs = {'type': 'hidden'}),
'address' : forms.TextInput(attrs = {'type': 'hidden'}),
'city' : forms.TextInput(attrs = {'type': 'hidden'}),
'lat' : forms.TextInput(attrs = {'type': 'hidden'}),
'lng' : forms.TextInput(attrs = {'type': 'hidden'}),
'trips' : forms.TextInput(attrs = {'type': 'hidden'}),
}
fields = ('name', 'google_id', 'address', 'city', 'lat', 'lng', 'trips')
如果我拿走id =&#34; save&#34;然后它完美地运作。