JS和HTML:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function load_bkissue(accessid){
//alert(accessid);
$.ajax({
type: "POST",
url: "2.php",
async: false,
dataType: "json",
data: "accession_id=" + accessid,
success: function (response) {
$("#bkid").val(response.accession_id);
$("#txtbnam").val(response.title);
$("#txauth").val(response.author);
$("#txtcat").val(response.category);
$("#txtsub").val(response.subject_type);
//$("#txt_div").val(response.rack_no);
$("#txtrack").val(response.rack_no);
}
});
</script>
</head>
<body>
<table width="260" id="tab" cellpadding="1" cellspacing="10">
<tr>
<td width="20px"> AccessionNo </td>
<td width="400px">
<select name="bkid" onChange="load_bkissue(this.value)" class="txtbox">
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
<option value="5">5 </option>
</select> </td>
</tr>
<tr>
<td> Title </td>
<td><input name="txtbnam" id="txtbnam" type="text" class="txtbox"></td>
</tr>
<tr>
<td> Author </td>
<td><input name="txauth" id="txauth" type="text" id="txauth" class="txtbox"></td>
</tr>
<tr>
<td> Category </td>
<td><input name="txtcat" id="txtcat" type="text" id="txtcat" class="txtbox"></td>
</tr>
<tr>
<td> Subject </td>
<td><input name="txtsub" id="txtsub" type="text" id="txtsub" class="txtbox"></td>
</tr>
<tr>
<td> Rack </td>
<td><input name="txtrack" id="txtrack" type="text" id="txtrack" class="txtbox"></td>
</tr>
</table>
</body>
</html>
PHP:
<?php
$json = array(
'accession_id' => "a",
'title' => "b",
'author' => "c",
'category' => "d",
'subject_type' => "e",
'rack_no' => "f");
echo json_encode($json );
?>
选择AccessionNo
时,我需要在标题,作者等各个字段中包含这些值。但这不起作用。
答案 0 :(得分:1)
你的JSON,是......很奇怪。为什么不这样做:
<?php
$json = array(
'accession_id' => "a",
'title' => "b",
'author' => "c",
'category' => "d",
'subject_type' => "e",
'rack_no' => "f");
echo json_encode($json);
?>
这导致JSON看起来像这样(更加理智):
{
"accession_id":"a",
"title":"b",
"author":"c",
"category":"d",
"subject_type":"e",
"rack_no":"f"
}
让您的javascript 更多更简单:
$.ajax({
type: "POST",
url: "http://temp.lmfast1/testajax/2.php",
async: false,
dataType: "json",
data: "accession_id=" + accessid,
success: function (response) {
$("#bkid").val(response.accession_id);
$("#txtbnam").val(response.title);
$("#txauth").val(response.author);
$("#txtcat").val(response.category);
$("#txtsub").val(response.subject_type);
//$("#txt_div").val(response.rack_no);
$("#txtrack").val(response.rack_no);
}
});
答案 1 :(得分:0)
你的ajax电话缺少一些重要的事情。试试这个电话
function load_bkissue(accessid) {
var myID = { accession_id : accessid };
var DTO = JSON.stringify(myID);
$.ajax({
type: "POST",
url: "2.php", // /testajax/2.php
contentType: "application/json",
dataType: "json",
data: DTO,
success: function(response) //'response' is the output provided by the controller method
{
alert(response);
$.each(response, function(i, item) {
if (item.field == "accession_id") {
$("#bkid").val(item.value);
} else if (item.field == "title") {
$("#txtbnam").val(item.value);
} else if (item.field == "author") {
$("#txauth").val(item.value);
} else if (item.field == "category") {
$("#txtcat").val(item.value);
} else if (item.field == "subject_type") {
$("#txtsub").val(item.value);
} else if (item.field == "rack_no") {
//$("#txt_div").val(item.value);
$("#txtrack").val(item.value);
}
});
}
});
}
这些是我在您的代码中看到的问题。
contentType
dataType: 'json'
,但您的数据不是json类型。