当使用ajax选项的值为square时,我想提醒这是一个正方形。但我的问题是,当我进入页面并选择方形选项时,没有任何反应,需要刷新以提醒这是正方形。
这是我的脚本,它检查是否设置了square值,以便ajax可以处理它。
$(document).ready(function () {
if($('#SelectBox').val() == 'square'){
$.ajax({
type:'post',
url:'views/index/test',
data:{},
success:(function (response) {
alert(response)
})
})
}
})
这是我的test.php
<?php echo "this is square"?>
这些是我的标签
<select name="selected_option" id="SelectBox">
<option value="default" selected="selected">Select one option </option>
<option value="square">Square</option>
<option value="rectangle">Rectangle</option>
</select>
答案 0 :(得分:0)
您在页面加载时运行一次支票。相反,将事件处理程序连接到change
:
select
事件
$(document).ready(function() {
$('#SelectBox').on("change", function() { //****
if ($('#SelectBox').val() == 'square') { // Can use `$(this).val()` instead here
$.ajax({
type: 'post',
url: 'views/index/test',
data: {},
success: (function(response) {
alert(response)
});
});
}
});
});