我想在用戶在#sid字段中輸入值之後立即用數據庫中的值填充表單字段。這裏是我的jQuery/HTML例如:jQuery JSON問題
<script src="jquery-1.3.1.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function()
{
$('#sid').bind("change", function(){
$.getJSON("test.php?sid=" + $("#sid").val(),
function(data)
{
$.each(data.items,
function(i, item)
{
if (item.field == "saffil")
{
$("#saffil").val(item.value);
}
else if (item.field == "sfirst")
{
$("#sfirst").val(item.value);
}
});
});
});
});
</script>
這是我處理腳本(test.php的它獲取由.getJSON方法調用)
<?
require_once("db_pers.inc");
$ssql = "SELECT * FROM contacts_mview WHERE sempid = '".$_GET['sid']."'";
$rres = pg_query($hdb, $ssql);
pg_close($hdb);
$ares = pg_fetch_assoc($rres);
$json = array(array('field' => 'saffil',
'value' => $ares['saffil']),
array('field' => 'sfirst',
'value' => $ares['sfirst']));
echo json_encode($json);
?>
根據螢火蟲的GET參數是通過就好了到test.php的和JSON對象回來就好了:
[{"field":"saffil","value":"Admin"},{"field":"sfirst","value":"Nicholas"}]
但是什麼也沒有發生在頁面上,我收到以下錯誤信息回:
G is undefined
init()()jquery-1....1.min.js (line 12)
(?)()()test.html (line 15)
I()jquery-1....1.min.js (line 19)
F()()jquery-1....1.min.js (line 19)
[Break on this error] (function(){var l=this,g,y=l.jQuery,p=l.....each(function(){o.dequeue(this,E)})}});
這是我與jQuery阿賈克斯首次嘗試,所以任何輸入將不勝感激!
感謝,
- 尼古拉斯
順便說一句,當你打開/關閉腳本標記在同一行時,它會截斷你的代碼。打破他們到單獨的線路。 – bendewey 2009-02-18 03:08:56