我试图通过调用函数getId在ajax上显示消息警报。我正在使用选择选项列表。但是当我单击选项列表时,不会显示警告消息。那是为什么?
这是我的代码:
<?php
require_once "connection.php";
?>
<html>
<head>
<title>Dropdown ajax</title>
</head>
<body>
<label>Country</label>
<select name="country">
<div class="country" onchange="getId(this.value);">
<option value=""> -- Select Country -- </option>
<!-- populate value using php -->
<?php
$query = "SELECT * FROM country";
$result = mysqli_query($con,$query);
//loop
foreach ($result as $country) {
?>
<option value="<?php echo $country["cid"]; ?>"> <?php echo $country["country"]; ?> </option>
<?php
}
?>
</select>
</div>
<div class="city">
<label>City</label>
<select name="city">
<option value=""> -- Select City -- </option>
</select>
</div>
<script type="text/javascript">
function getId(val){
alert(val);
}
</script>
</body>
</html>
有谁知道如何解决这个问题?
答案 0 :(得分:1)
<select name="country">
<div class="country" onchange="getId(this.value);">
应该是
<div class="country">
<select name="country" onchange="getId(this.value);">