我想返回json数据,但我的代码不起作用.我没有收到任何错误消息.我有index.php,ajax.php和db.php.Db.php正在工作.但我的ajax代码无效.我的错误在哪里?
index.php文件:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
</head>
<body>
<div id="test" style="width:500px;height:400px; margin:20px auto;"></div>
<script>
$(window).load(function () {
$.ajax({
dataType: "json",
url: 'ajax.php',
success:function(data){
$("#test").html(data);
}
});
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Ajax.php:
<?php
require 'db.php';
$query="select lat,lng from locations order by id";
$result = pg_query($link, $query);
if (!$result) {
echo "An error occurred_xxx.\n";
}else {
$arr = pg_fetch_all($result);
echo json_encode($arr);
} ?>
Run Code Online (Sandbox Code Playgroud) 我正在使用自举开关按钮.我希望在文本或文本更改时更改输入值.如何检查on-text ="Android"我是否要将android打印到inp1文本,或者如果off-text ="IOS"我想将ios打印到inp1文本.有另一种检查文本的方法吗?你可以查看在线演示http://jsfiddle.net/PNU45/156/
HTML
<input type="input" id="inp1" value="">
<input type="input" id="inp2" value="">
<input type="checkbox" checked="true" class="alert-status" id="btn1" data-size="normal" name="my-checkbox" data-on-text="Android" data-off-text="IOS">
<input type="checkbox" checked="true" class="alert-status" id="btn2" data-size="normal" name="my-checkbox" data-on-text="Java" data-off-text="C#">
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$('.alert-status').bootstrapSwitch('state', true);
$('#btn1').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Android"){
$("#inp1").val("Android");
}
if(y=="IOS"){
$("#inp1").val("IOS");
}
});
$('#btn2').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Java"){
$("#inp2").val("Java");
}
if(y=="IOS"){
$("#inp2").val("IOS");
}
});
Run Code Online (Sandbox Code Playgroud)