preventDefault not work



  • When selecting the values from the first seltration, the values in the second selt; But if you choose another meaning from the first seltzer, the values in the second selt; the old ones don't purify.

    <div class="form-group">
                    <div class="row">
                        <label for="name" class="col-sm-3 control-label">Авто <span class="text-danger">*</span></label>
                        <div class="col-sm-4">
                            {!! Form::select('car', $cars, null, ['class' => 'form-control']) !!}
                        </div>
                        <div class="col-sm-5">
                            <select name="car_options" class="form-control">
    
                        &lt;/select&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
    

    Code itself

    <script type="text/javascript">
    $('select[name=car]').change(function(e) {
    var car_option_id = $('select[name=car_options]');
    e.preventDefault();
    $.ajax({
    type: 'post',
    dataType: 'json',
    url: 'url',
    data: {
    car_id: $('select[name=car]').val()
    },
    headers: {
    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    },
    success: function(data){
    $.each(data, function(index, car_option) {
    car_option_id.append("<option value=" + car_option.id + ">" + car_option.name +"</option>")
    });
    console.log(data);
    }
    });
    });
    </script>



  • car_option_id.append
    

    appends to the existing, not rewrite, I'd, in your place, first cleanse the select like this car_option_id.empty();

            success: function(data){
    
            car_option_id.empty();
            $.each(data, function(index, car_option) {
                car_option_id.append("&lt;option value=" + car_option.id + "&gt;" + car_option.name +"&lt;/option&gt;")
            });
            console.log(data);
        }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2