Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent form submission for a form using onchange to submit the form when certain values are selected

I have a form I have built:

<form class="myform" action="cgi.pl">
  <select name="export" onchange='this.form.submit()'> 
    <option value="" selected="selected">Choose an export format</option> 
    <option value="html">HTML</option> 
    <option value="csv">CSV</option> 
  </select>
</form>

Now, this form works fine if I pull down and select "HTML" or "CSV". But if I hit the back button and select "Choose an export format", the form is submitted, even though I dont want it to be.

Is there any way to prevent form submission for that option?

like image 535
Terrence Brannon Avatar asked Oct 26 '25 11:10

Terrence Brannon


2 Answers

onchange='if(this.options[this.selectedIndex].value!=''){ this.form.submit(); }'
like image 142
oezi Avatar answered Oct 28 '25 02:10

oezi


$("select[name=export]").on("change", function(e) {
    if ($(this).val() != "") {
        $("form.myform").submit();
      }
});
like image 32
Ranish Karim Avatar answered Oct 28 '25 00:10

Ranish Karim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!