如何在单击 li 项时通过 AJAX 传递 li 标签 ID
How to pass the li tag id via AJAX when clicking the li item?
我想传递id值以在单击li标签值时显示城市函数,接受任何相关方法
<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>
我想传递 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);
}
});
});
});
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Ajax发布表单序列化,发布引号'
- 通过Ajax将JavaScript函数传递给PHP文件
- ajax请求的顺序总是不同的
- 可以't使用Polymer's的核心ajax
- Ajax Live搜索发布到Laravel视图
- Ajax聊天消息重复而不仅仅是更新
- 从控制器返回后Ajax启动事件激发
- PHP AJAX图片上传示例不上传
- Rails-使用Ajax删除li元素
- 如何遍历 UL 中的 LI 项,获取特定属性,并通过 $.ajax 数据传递它们
- 如何在单击 li 项时通过 AJAX 传递 li 标签 ID
- 在 ajax 成功后删除最接近的 li
- jQuery / Ajax 将类添加到 LI 不起作用
- 当从ajax添加元素时,Li动作不工作
- Javascript: DOM有多个li,每个li有一个表单,该表单必须通过ajax提交