我如何通过选择字段ID和它的值ajax没有任何形式


How can I pass select field ID and its value to ajax without having any form?

我有一个选择字段,其ID名称为'region_code'及其值。我想在ajax中传递ID。如下所示,输入字段不包含在任何表单中。它有一个值和ID

是否有可能在ajax中获得如下所示的值?

  echo '<select id="region_code" onchange="show_region_code();">';
  $result = mysql_query("SELECT region_code, region_name FROM list_region");
  while($rows = mysql_fetch_array($result)) {                      
             echo "<option value='"$rows[0]'">".$rows["1"].'</option>';
  }
  echo '</select>';

ajax函数如下

function show_region_code() {
     var region_code = $("#region_code").val();
     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: { region_code1: region_code },
              success: function(data) {
                     $("#region_code").html(data);
            }
     });
 }

使用

<select id="region_code" onchange="show_region_code(this);">
    <option value="1">te</option>
    <option value="2">te2</option>
</select>
function show_region_code(Obj)
{
    alert(Obj.id);
}

看这是你的代码和工作:

<?php 
echo '<select id="region_code" onchange="show_region_code();">';
$rows=0;
  //$result = mysql_query("SELECT region_code, region_name FROM list_region");
  while($rows !=2) { 
    $rows++;
             echo "<option value=$rows>".$rows.'</option>';
  }
  echo '</select>';
?>
<script>
function show_region_code() {
     var region_code = $("#region_code").val();
alert(region_code);
     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: { region_code1: region_code },
              success: function(data) {
                     $("#region_code").html(data);
            }
     });
 }
</script>
<?php         
   echo '<select id="region_code" onchange="show_region_code();">';
   $result = mysql_query("SELECT region_code, region_name FROM list_region");
   while($rows = mysql_fetch_array($result)) {                      
      echo "<option value=".$rows[0].">".$rows[1]."</option>";
   }
   echo '</select>';
?>
function show_region_code() {
     var region_code = $("#region_code").val();
     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: "region_code1="+region_code,
            success: function(data) {
               $("#region_code").html(data);
            }
     });
 }