[英]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.我正在尝试使用 ajax 提交所选值以避免页面加载。 even with the "return false;"即使使用“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(); FIX:同时删除:location.reload(); and return false;并返回假; fixed the issue as mentioned by @JayBlanchard解决了@JayBlanchard 提到的问题
Remove location.reload();
删除location.reload();
, this causes the page to reload when the AJAX succeeds. ,这会导致页面在 AJAX 成功时重新加载。 Additionally there is no need for return false;
此外,不需要return false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.