简体   繁体   中英

Trying to submit data using Ajax without page load with on change event of select

I am trying to submit the selected value using ajax to avoid a page load. even with the "return false;" there is still a page load. Data is posted successfully, but the page still refreshes.

<div style='display:none;' id='RCTruckTH' name='RCTruckTH'>
<br><label>Height in meters *</label>
    <select class="form-control" style="max-width:300px!important;" id='RCTruckHV' name='RCTruckHV'>
         <option <?php if(isset($_SESSION['RCTruckHV']) && $_SESSION['RCTruckHV'] == "1.8m") echo "selected";?>>1.8m</option>
         <option <?php if(isset($_SESSION['RCTruckHV']) && $_SESSION['RCTruckHV'] == "1.9m") echo "selected";?>>1.9m</option>
         <option <?php if(isset($_SESSION['RCTruckHV']) && $_SESSION['RCTruckHV'] == "2.0m") echo "selected";?>>2.0m</option>
    </select>
</div>
$(document).ready(function(){
      $('#RCTruckHV').on('change', function() {
            var rcheival = $selected_value=$('#RCTruckHV option:selected').text();
            localStorage.setItem('rcheival', rcheival);
            //console.log(rcheival);

                $.ajax({
                   url: 'RDSetSession.php',
                   type: 'get',
                   data: {
                             'rcheival': rcheival
                         },
                         success: function() {
                         location.reload();
                     }
               });
           return false;
     });
});

FIX: Removing both: location.reload(); and return false; fixed the issue as mentioned by @JayBlanchard

Remove location.reload(); , this causes the page to reload when the AJAX succeeds. Additionally there is no need for return false;

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