Javascript 单击li项时如何通过AJAX传递li标记id?
我想通过id值来显示城市功能,只要点击li标签值,任何相关方法都可以接受Javascript 单击li项时如何通过AJAX传递li标记id?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我想通过id值来显示城市功能,只要点击li标签值,任何相关方法都可以接受 <script type = "text/javascript" > function showCity(str) { if (str == "") { document.getElementById("city").innerHTML = ""; return; } else { if (window.XMLHttpRequest) {
<script type = "text/javascript" >
function showCity(str) {
if (str == "") {
document.getElementById("city").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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("city").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "city.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
<div id="menu">
<ol id="test">
<?php $stat=m ysql_query( "select * from `tms_state`"); while (($st=m ysql_fetch_array($stat))) { ?>
<li onclick="showCity('this.value')">
<?php echo "<option value='" . $st[ 'id'] . "'>" . $st[ 'name'] . "</option>";?>
</li>
<?php } ?>
</ol>
</div>
功能展示城市(str){
如果(str==“”){
document.getElementById(“城市”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“city”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“city.php?q=“+str,true”);
xmlhttp.send();
}
}
我想传递id值,以便在单击li标记值时显示城市功能。
<div id="menu">
<ol id="test">
<?php
$stat=m ysql_query( "select * from `tms_state`");
while (($st=m ysql_fetch_array($stat))) { ?>
<li onclick="showCity('<?php echo $st[ 'id'] ?>')">
<?php echo "$st[ 'id']";?>
</li>
<?php } ?>
</ol>
</div>
使用jQuery可以更轻松地完成此任务-
$(document).ready(function(){
$('#test li').click(function () {
var str = $(this).val();
$.ajax(
{
url: "city.php?q=" + str,
success: function (result) {
$("#city").html(result);
}
});
});
});
请帮我做什么我已经做了很长时间了你到底在找什么?请让我知道。单击您需要的文本的 标记后,我理解了什么?我使用的是li项值,该值来自我单击该项时所需的数据库,其id值应输入showcity(this.value)因此,可以将其分配给ajax参数str。现在,它将this.value作为字符串来代替li项标记值idno我想要单击showcity()函数时的id值请检查firebug,ajax调用是否正在进行。我认为这个函数不会起作用。因为li在这里没有value属性