【发布时间】:2016-01-29 02:17:20
【问题描述】:
我有一个复选框,并且希望当它被选中/选中时,应该加载来自另一个文件(即 c_datetime.php)的值,为此我使用了 ajax,当我选中/选中复选框时,值是getting loaded properly but when i uncheck it the loader gets loaded again and the ajax displays the data again, whereas the proper way should have been that when the checkbox is selected the values should get loaded and when its unselected the values should disappear, can anyone请说明为什么它没有按应有的方式工作
<script>
$(document).ready(function(){
$('#drive').change(function(){
var catid = $('#drive').val();
console.log($('#drive'))
if(catid != 0)
{
LodingAnimate();
$.ajax({
type:'post',
url:'c_datetime.php',
data:{id:catid},
cache:false,
success: function(returndata){
$('#datetime').html(returndata);
console.log(returndata)
}
});
function LodingAnimate()
{
$("#datetime").html('<img src="img/ajax-loader.gif" height="40" width="40"/>');
}
}
}
)
})
</script>
<div id="datetime"> </div>
<input type="checkbox" name ="chk" id="drive" value="Drive" >Drive
【问题讨论】:
-
你应该检查复选框是否只选中然后加载数据添加
if($(this).is(':checked'))condition
标签: javascript php jquery ajax checkbox