1

i have a simple html form with 3 drop downs (select with options)

on submit i want to not deliver the empty variable.

for example:

<select name="numbers"> 
        <option></option>
        <option>11</option>
        <option>22</option>
        <option>33</option>
</select>
<select name="letters"> 
        <option></option>
        <option>aa</option>
        <option>bb</option>
        <option>cc</option>
</select>

if the form is send I get numbers and letters with an empty value.

in this case the variable numbers and letters should be deleted and not be posted to the action form

divibisan
  • 11,659
  • 11
  • 40
  • 58
dp303
  • 11
  • 2
  • Are you using AJAX or the default form submission behaviour? – Matt Jun 20 '17 at 14:43
  • Are you Posting this to a php File ? Cause it'll be very easy to handle it on the server side! – Hamza Abdaoui Jun 20 '17 at 14:44
  • 1
    Possible duplicate of [jquery: how to remove blank fields from a form before submitting?](https://stackoverflow.com/questions/5904437/jquery-how-to-remove-blank-fields-from-a-form-before-submitting) – oguzhancerit Jun 20 '17 at 14:51

2 Answers2

0

add selected disabled to your default empty options:

<select name="numbers"> 
        <option selected disabled></option>
        <option>11</option>
        <option>22</option>
        <option>33</option>
</select>
<select name="letters"> 
        <option selected disabled></option>
        <option>aa</option>
        <option>bb</option>
        <option>cc</option>
</select>
WheatBeak
  • 1,036
  • 6
  • 12
0
 $('select option').each(function(){
            if($(this).text() == '' || $(this).text() == null)
                $(this).remove();
    })

or 
 $('[name="numbers"] option').each(function(){
            if($(this).text() == '' || $(this).text() == null)
                $(this).remove();
   })

 $('[name="letters"] option').each(function(){
            if($(this).text() == '' || $(this).text() == null)
                $(this).remove();
  })
Biplab Malakar
  • 750
  • 6
  • 11