django submit form on change
Asked Answered
P

2

6

Hello I am trying to submit a form when selection an option on a ChoiceField

class ActionForm(forms.Form):
    """ Holds the options for mailbox management """
    choices = ['create new folder', 'delete', 'read', 'unread']
    action = forms.ChoiceField(choices=choices, attrs={'onchange': 'actionform.submit();'})

but now I get an invallid syntax when I try to load the form. I am pretty sure the attrs={'onchange': 'actionform.submit();'}) is the problem, but not sure how else to do it.

Phantom answered 29/5, 2014 at 21:55 Comment(0)
O
9

You need to set a widget argument on the field and pass attrs argument:

action = forms.ChoiceField(choices=choices, 
                           widget=forms.Select(attrs={'onchange': 'actionform.submit();'}))

Also, the choices list should contain items containing two things inside:

choices = [(0, 'create new folder'), (1, 'delete'), (2, 'read'), (3, 'unread')]
Obsolete answered 29/5, 2014 at 22:1 Comment(1)
thanks for the awnser. this seems to work, however now i think my choices is still wrong. i get the error: To many values to unpackPhantom
N
0

In the template-html file’s form tag include the attribute

onchange="functionName()” to call:

function functionName(){
   var submitButton = 
document.getElementById(“the id of the button”);
   submitButton.click();
}

Give the submit button and id, for example:

input type="submit" value="Submit" id="myButton"

With CSS set the button’s visibility to:

#myButton{
   visibility: hidden;
}
Nombles answered 15/6, 2022 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.