Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent to serialize input

Tags:

html

jquery

I have select option box.

<form class="i-hate-u">
   <select class="helloMoto" name="helloMoto">
      <option value="">Select...</option>
      <option value="-1">Other</option>
   </select>
</form>

When the end-user select "other" option, text input appear like this :

$('.helloMoto').on('change', function() {
      if ($(this).val() == '-1')
            $(this).hide();
            var input_name = $(this).attr('name');
            var input = '<div class="input-group">' +
                             '<input type="text" class="form-control" name="'+input_name+'" placeholder="Type other.">' +
                              '<span class="input-group-btn">' +
                                     '<button class="btn btn-primary" onclick="removeSelectOther(this);"><i class="la la-close"></i></button>' +
                              '</span>' +
                        '</div>';

            $(this).parent().append(input);
});

At the end of this, when the end-user click save button, i serialize whole form like this :

var data = $('.i-hate-u').serialize();

As you can see, if the user selected an other option, input appear with same name. I want to remove if user select other option. Is there any way to if serialized value has same name, skip first one or -1.

like image 988
Nevermore Avatar asked Sep 12 '26 14:09

Nevermore


1 Answers

Just before serializing, check if the value for helloMoto is -1 if yes then disable the <select> like(on form submit):

if($('select[name="helloMoto"]').val() == -1)
{
    $('select[name="helloMoto"]').attr('disabled', true);
}

Alternatively you can try, just exclude the helloMotowhile serialization:

if($('select[name="helloMoto"]').val() == -1)
{
    var data = $('input[name!=security]', $('.i-hate-u')).serialize();
}else 
{
    var data = $('.i-hate-u').serialize();
}
like image 124
mega6382 Avatar answered Sep 14 '26 04:09

mega6382



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!