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

I have a form that I created:

<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 great if I pulled out and selected "HTML" or "CSV". But if I click the "Back" button and select "Choose Export Format", the form will be sent, although I do not want it to be.

Is there any way to prevent form submission for this parameter?

+3
source share
2 answers
onchange='if(this.options[this.selectedIndex].value!=''){ this.form.submit(); }'
+4
source
$("select[name=export]").on("change", function(e) {
    if ($(this).val() != "") {
        $("form.myform").submit();
      }
});
+1
source

Source: https://habr.com/ru/post/1739889/


All Articles