<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function getvalues() {
var selname = $("input[name='names']:text").val();
$.ajax({
url: "process.php",
data: {
"selname": selname
},
type: 'post',
dataType: "json",
success: function (output) {
console.log(output);
$("#aic").val(output[0]);
$("#batchcode").val(output[1]);
}
});
}
</script>
</script>
</head>
<body>
<form method="post">
<input type="text" name="names" id="query" onblur="getvalues()" />
<input type="text" name="aic" id="aic" />
<textarea name="batchcode" id="batchcode" rows="4" cols="50"></textarea>
</form>
</body>
</html>
process.php
<?php
if(isset($_POST['selname']))
{
$response = array('0001', 'short info : john 29 years old - technician '); // add return data to an array
echo json_encode($response); // json encode that array
exit;
}
?>
当我在input type="text" name="names"
上输入内容时
我得到了输入文字"aic" >>> 1;
但是在textarea我没有得到"short info : john 29 years old - technician "
,请告诉我错误在哪里!!
答案 0 :(得分:1)
将json转换为数组
var array = JSON.parse(output)
$("#aic").val(array[0]);
$("#batchcode").val(array[1]);
答案 1 :(得分:1)
试试这个
function getvalues() {
var selname = $("input[name='names']:text").val();
$.ajax({
url: "process.php",
data: {
"selname": selname
},
type: 'post',
dataType: "json",
success: function (output) {
var output = jQuery.parseJSON( output );
console.log(output);//use output.property
$("#aic").val(output[0]);
$("#batchcode").val(output[1]);
}
});
}