我有一个下拉菜单,其中包含来自其他下拉选项的详细信息。我通过在第一个下拉菜单中获取所选选项的详细信息并将其传递给位于db中的MYSQLI函数,从第一个下拉选项中读取数据并将数据传递到第二个下拉列表时遇到问题class,使用该细节来运行查询并将结果返回给ajax,然后使用该结果填充下一个dropdwon
<div class="form-group">
<label class="col-sm-3 control-label">State</label>
<div class="col-sm-5">
<select name="state" class="form-control select2" style="width:100%;" data-validate="required" id="State"
data-message-required="State Required" onchange="return get_state_cities(this.value)">
<option value="">Select</option>
<?php
$notarray = DataDB::getInstance()->select_from('state');
foreach($notarray as $row):
?>
<option value="<?php echo $row['state'];?>"> <?php echo $row['state'];?> </option>
<?php
endforeach;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">City</label>
<div class="col-sm-5">
<select name="city" class="form-control" id="city_selector_holder">
<option value="">Select State First</option>
</select>
</div>
</div>
AJAX功能
function get_state_cities(state) {
$.ajax({
url: 'db.php/get_state_city/' + state,
success: function(response)
{
jQuery('#city_selector_holder').html(response);
}
});
}
db.php函数
function get_state_city($state)
{
$cities = $this->query("SELECT * FROM city where state ='".$state."'");
foreach ($cities as $row) {
echo '<option value="' . $row['city'] . '">' . $row['city'] . '</option>';
}
}