[英]Get HTML id with JQuery through AJAX
I'm stuck..I have made a system which gets all colleges from a specific subject, and I want javascript to select the college the user has selected when he clicks on one so the presence of that class can be shown, just a summary: the user selects a subject, an AJAX request gets all colleges from that subject and another AJAX request has to get that specific college and use it to display the data. 我被困了..我已经建立了一个系统,可以从特定主题中获取所有大学,我希望javascript选择用户在点击一个大学时选择的大学,这样就可以显示该类的存在,只是一个摘要:用户选择一个主题,一个AJAX请求获取该主题的所有大学,另一个AJAX请求必须获得该特定大学并使用它来显示数据。
This specific function gets the data from the subjects and displays them in a div with the id 'colleges' 此特定函数从主题中获取数据并将其显示在ID为“学院”的div中
function showAanwezigheid(str) {
if (str == "") {
document.getElementById("colleges").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("colleges").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","../controller/docentController.php?action=getColleges&vak="+str,true);
xmlhttp.send();
}
}
Here is the controller which calls the query and displays the result: 这是调用查询并显示结果的控制器:
$vak = $_GET['vak'];
$colleges = $q->getColleges($vak);
echo "<select id='college'>";
echo "<option>Selecteer een college:</option>";
foreach($colleges as $college){
echo "<option value='".$college->getCollege()."'>College ".$college->getCollege()."</option>";
}
echo "</select>";
}
This is the id where the result is being displayed 这是显示结果的id
<span id="colleges"></span>
And this function should select the 'select' tag with the id 'college' to get that value on change 此函数应选择id为'college'的'select'标记,以便在更改时获得该值
$(document).ready(function(){
$('#vak').on('change', function() {
var vak = this.value;
alert(vak);
$('#college').on('change', function() {
var college = this.value;
alert(college);
});
});
});
I feel like there would be such an easy fix but I just can't seen to make it work... 我觉得会有这样一个简单的修复,但我只是看不到让它工作......
Change jQuery code like below:- 更改jQuery代码如下: -
$(document).on('change','#vak',function() {
var vak = $(this).val();
alert(vak);
});
$(document).on('change','#college',function() {
var college = $(this).val();
alert(college);
});
Note:- Make sure that jQuery library added before your script code. 注意: - 确保在脚本代码之前添加了jQuery库。 Otherwise you will get
$ is undefined
error 否则你将得到
$ is undefined
error
This is called Event Delegation 这称为事件委派
IF you want to get the value of the #college select
on change then this would be simpler: 如果你想在变化时获得
#college select
的值,那么这将更简单:
$(document).ready(function(){
$('#vak').on('change', function() {
var vak = $(this).val();
alert(vak);
});
$('#college').on('change', function() {
var college = $(this).val();
alert(college);
});
});
IF you noticed, The onchange
of the #vak
and the #college
is separated 如果你注意到,
#vak
和#college
的onchange
是分开的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.