在这个测试中,我有一个带有两个字段的简单表单。当用户在选择字段中选择一个选项时,我需要更新文本字段。
这是html代码:
<form id="form1" name="form1" method="post" action="">
<input type="text" name="dst" id="dst" value="" />
<select name="contactos" id="contactos">
<option value="-1">No selected</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</form>
这是jquery代码:
<script>
$(document).ready(function() {
$("#contactos").change(function(event){
var id = $("#contactos").find(':selected').val();
$.getJSON('contacback.php?contactoID='+id, function(data) {
$.each(data, function(i,item) {
if (item.field == "dst") {
$("#dst").val(item.value); }
});
});
});
});
</script>
(已添加)这是php代码:
<?php require_once('Connections/cnx3.php'); ?>
<?php
$colname_rsContactos = "-1";
if (isset($_GET['contactoID'])) {
$colname_rsContactos = $_GET['contactoID'];
}
mysql_select_db($database_cnx3, $cnx3);
$query_rsContactos = sprintf("SELECT contactoID, telefono FROM contactos WHERE contactoID = %s",
GetSQLValueString($colname_rsContactos, "int"));
$rsContactos = mysql_query($query_rsContactos, $cnx3) or die(mysql_error());
while ($fila = mysql_fetch_array($rsContactos)) {
$json = array('field' => 'dst',
'value' => $fila['telefono']);
}
echo json_encode($json);
mysql_free_result($rsContactos);
?>
对php的查询发送正常,JSON回答它也没关系 image of JSON reply
但#dst的值未更新。欢迎任何帮助!!
答案 0 :(得分:0)
(在问题编辑和评论中回答。转换为社区维基答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat))
OP写道:解决!!感谢@Attack
jQuery
脚本:
$("#contactos").change(function(event){
var id = $("#contactos").find(':selected').val();
$.getJSON('contacback.php?contactoID='+id, function(data) {
if (data.field == "dst") {
$("#dst").val(data.value);
}
});
});