简体   繁体   中英

Submit form on change of dropdown only when special value is selected

I have a dropdown list with a few options (eg 7). The form should be submitted when one of the last 3 options is selected. On the first 4 options the form should not be submitted.

onChange="this.form.submit()" does not help - the form will be submitted on every change...

Any ideas?

Thanks a lot in advance

Hias

Do...

onchange="checkVal();"

Then in your JS make the function...

function checkVal{
    var e = document.getElementById("FORMID");
    var selectedVal = e.options[e.selectedIndex].value;
    if (selectedVal == val4 || selectedVal == val5 || selectedVal == val6){
         e.submit();
    }
}

This will work if you have your options set up with values <option val="4">number 4</option> If not you have to use .text instead of .value to get the selectedVal and then compare the text inside the options

It's simple check the value of select element if it's what you want then tell form to submit itself.

Demo of this : http://jsfiddle.net/Q6FUM/

var f = document.forms['mf'], s= document.getElementById('ss');

s.onchange = function() {
    if (s.value==3) f.submit();
}

Thanks a lot. It works with this code:

function checkVal() {
  var f = document.getElementById("FORM-ID");
  var e = document.getElementById("SELECT-ID");
  var selectedVal = e.options[e.selectedIndex].value;
  if (selectedVal > 4) {
    f.submit();
  }
}

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