AJAX 填充 select 在网络服务器中不工作
AJAX to populate select not working in webserver
我编写了以下代码来将选项填充到 select 列表中。如果一个选项列表 (id="course_id") 中有一个 selects "course",它会触发 onchange 事件。然后 AJAX get 方法调用 php 程序并传递课程 ID。 PHP 程序生成一个字符串作为
<option id="1">Subject1</option> <option id="2">Subject2</option>....
我想将此字符串分配给另一个 select 列表。问题是程序在本地电脑运行正常。但是在网络服务器中,第一条测试消息出现了 3 次,然后显示了消息 2。但是消息 2 没有执行,也没有加载数据。请帮忙!
$('#course_id').change(function(e) {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var first = document.getElementById('course_id');
var course_id = first.options[first.selectedIndex].value;
var param = course_id;
var url = "adm_reposelectSql.php?course_id=" + param;
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = getThings;
xmlhttp.send();
});
function getThings(){
alert("software testing message 1!");
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("software testing message 2");
var a = xmlhttp.responseText;
$('#subject_id').html(a);
}
else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
alert("message 3 No items returned for request");
}
}
For your first doubt that the first message is being called 3 times
发生这种情况是因为 getThings
方法正在 onreadystatechange
上调用。就绪状态为 1、2、3、4(这是最终状态),对于每个值都调用此方法。
只有第四(4)个进入你的if条件
因此,在您的情况下,getThings
中的第一条语句执行
readyState = 1
readyState = 2
readyState = 3
因此调用了3次
我编写了以下代码来将选项填充到 select 列表中。如果一个选项列表 (id="course_id") 中有一个 selects "course",它会触发 onchange 事件。然后 AJAX get 方法调用 php 程序并传递课程 ID。 PHP 程序生成一个字符串作为
<option id="1">Subject1</option> <option id="2">Subject2</option>....
我想将此字符串分配给另一个 select 列表。问题是程序在本地电脑运行正常。但是在网络服务器中,第一条测试消息出现了 3 次,然后显示了消息 2。但是消息 2 没有执行,也没有加载数据。请帮忙!
$('#course_id').change(function(e) {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var first = document.getElementById('course_id');
var course_id = first.options[first.selectedIndex].value;
var param = course_id;
var url = "adm_reposelectSql.php?course_id=" + param;
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = getThings;
xmlhttp.send();
});
function getThings(){
alert("software testing message 1!");
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("software testing message 2");
var a = xmlhttp.responseText;
$('#subject_id').html(a);
}
else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
alert("message 3 No items returned for request");
}
}
For your first doubt that the first message is being called 3 times
发生这种情况是因为 getThings
方法正在 onreadystatechange
上调用。就绪状态为 1、2、3、4(这是最终状态),对于每个值都调用此方法。
只有第四(4)个进入你的if条件
因此,在您的情况下,getThings
中的第一条语句执行
readyState = 1
readyState = 2
readyState = 3
因此调用了3次