我正在加载客户列表,我想根据选择的客户加载第二个下拉列表。出于某种原因,我的代码不起作用。这是我的代码:
<span style="display:inline-block;">
<select name="sCustomer" id="sCustomer" onChange="findLocations(this.value)">
<option value="0">- Select Customer -</option>
</select>
</span>
<span style="display:inline-block;">
<select id="sLocation" name="sLocation">
<option value="0">- Select Location -</option>
</select>
</span>
<script type="text/javascript">
function findLocations(custID) {
$('#sLocation').empty();
$('#sLocation').append("<option value='0'>- Select Location -</option>");
$.ajax({
type:"POST",
url:"getLocations.php",
contentType:"application/json; charset=utf-8",
data: { custID : custID },
dataType:"json",
success: function(data){
$.each(data,function(i, item){
$('#iTest').val("bobby");
$('#sLocation').empty();
$('#sLocation').append("<option value='0'>- Select Location -</option>");
$('#sLocation').append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
});
},
complete: function(){
}
});
}
</script>
并且它调用的页面具有此代码
<?php
include_once(INCLUDE_DIR.'class.error.php');
include_once(INCLUDE_DIR.'class.role.php');
include_once(INCLUDE_DIR.'class.passwd.php');
include_once(INCLUDE_DIR.'class.user.php');
include_once(INCLUDE_DIR.'class.auth.php');
include_once(INCLUDE_DIR.'class.location.php');
function getLocations() {
$custID = 0;
if (isset($_POST['custID'])) {
$custID = $_POST['custID'];
}
$sql = 'SELECT l.location_id, l.site_name FROM ' . CUST_LOCATION_TABLE
. ' AS l JOIN ' . CUST_TABLE . ' AS c ON c.cust_id=l.cust_id'
. ' WHERE c.cust_id='.$custID;
$locations = array();
if (($res=db_query($sql)) && db_num_rows($res)) {
while(list($id,$name)=db_fetch_row($res)) {
$columns = array (
'locID' => $id,
'locName' => $name,
);
$locations[] = $columns;
}
}
return $locations;
}
?>
为什么这不起作用,无论如何都要测试哪个部分正在破坏?我无法回应任何事情,因为页面没有回发(因为它是ajax),我无法在ajax函数中执行javascript警报。请记住,我没有添加填充客户下拉列表的代码,因为我知道它正常工作,填充所有内容并给我正确的值。
好的,我发现这至少可以帮助我开始调试。它失败但我发现了这个
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
},
答案 0 :(得分:0)
替换
data: { custID : custID },
与
data: { 'custID' : custID },
在javascript中
答案 1 :(得分:0)
用这个替换你的php文件中的代码
if (($res=db_query($sql)) && db_num_rows($res)) {
$str = "";
$str .= "<option value='0'>Select Location</option>";
while(list($id,$name)=db_fetch_row($res)) {
$str .= "<option value=".$id.">".$name."</option>";
}
}
echo $str;
现在修改你的javascript函数
function findLocations(custID) {
$('#sLocation').empty();
$.ajax({
type:"POST",
url:"getLocations.php",
contentType:"application/json; charset=utf-8",
data: { custID : custID },
dataType:"json",
success: function(data){
document.getElementById("sLocation").innerHTML = data;
},
complete: function(){
}
});
}
答案 2 :(得分:0)
function findLocations(custID) {
$('#sLocation').empty();
$.ajax({
type:"POST",
url:"getLocations.php",
contentType:"application/json; charset=utf-8",
data: { custID : custID },
dataType:"json",
success: function(data){
document.getElementById("sLocation").innerHTML = "<option value='0'>- Select Locationss -</option>"; // + data;
},
complete: function(){
}
});
}
,php页面是
<?php
include_once(INCLUDE_DIR.'class.error.php');
include_once(INCLUDE_DIR.'class.role.php');
include_once(INCLUDE_DIR.'class.passwd.php');
include_once(INCLUDE_DIR.'class.user.php');
include_once(INCLUDE_DIR.'class.auth.php');
include_once(INCLUDE_DIR.'class.location.php');
$custID = 0;
if (isset($_POST['custID'])) {
$custID = $_POST['custID'];
}
$sql = 'SELECT l.location_id, l.site_name FROM ' . CUST_LOCATION_TABLE
. ' AS l JOIN ' . CUST_TABLE . ' AS c ON c.cust_id=l.cust_id'
. ' WHERE c.cust_id='.$custID;
$locations = array();
if (($res=db_query($sql)) && db_num_rows($res)) {
$str = "";
$str .= "<option value='0'>Select Location</option>";
while(list($id,$name)=db_fetch_row($res)) {
$str .= "<option value=".$id.">".$name."</option>";
}
}
echo $str;
?>