如何在单击 li 项时通过 AJAX 传递 li 标签 ID

How to pass the li tag id via AJAX when clicking the li item?

本文关键字:li AJAX 传递 ID 标签 单击      更新时间:2023-09-26

我想传递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);
                }
            });
      });
 });