简体   繁体   中英

django submit form on change

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.

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')]

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM