使用Ajax
Jquery 封装了操作Ajax的函数。 有三个函数是常用的: get, post 和 ajax。 ajax可以设置更多的参数,比另外2个好用。
由于Ajax调用的网址是外部的,所以函数执行有同步和异步之分别。异步执行时,函数立即返回。我不知道在异步模式下是否有消息通知的机制,目前还没有找到,如果有的话更新。
use as this:
$.get(url,function(data){
jsonObj = JSON.parse(data);
$("#txtStatus").text(
"DB connected. There is(are) "
+ jsonObj.Count
+ " record(s)." );
$(":input[name='prodList']").val(jsonObj.Products);
alert("Data Loaded: " + jsonObj);
});
If you want to use synchronous mode with Ajax, use as this:
$.ajax({
async: false,
type: "POST",
url: url,
contenttype: "application/json",
datatype: "json",
data: jsonObj,
success: function(jsonResult) {
$("#txtPrice").text(jsonResult);
}
});
Jquery的Selector
$("#txtStatus").text( "DB connected. Record count: " );
$(":input[name='btn-price']").click(function () {
var pname = $(":input#txtProd").val();
var url = "test1-hint.php?r=2&p=" + pname;
$.ajax({
async: false,
type: "POST",
url: url,
contenttype: "application/json",
datatype: "json",
data: jsonObj,
success: function(jsonResult) {
$("#txtPrice").text(jsonResult);
}
});
});