我有两个国家和州的列表框。当我点击一个国家的时候。印度,相应的州出现在州名单中。但同样的情况应该发生在一个以上的国家。如果我选择两个国家,这两个国家的州应该加入州名单。
国家/地区列表的javascript代码 -
$('#pcountries').on('change', function() {
var country_id = this.value;
$.ajax({
type: "POST",
url: "php_includes/select_list_pstate.php",
data: 'country_id=' + country_id,
success: function (result) {
$("#pstate").append(result);
}
});
});
php code -
if ($_POST['country_id']) {
$country_id = $_POST['country_id'];
if ($country_id != '') {
$states_result = $db_conx->query('select * from states where countryId=' . $country_id . '');
$options = "<option value=''>Any</option>";
while ($row = $states_result->fetch_assoc()) {
$options .= "<option value='" . $row['stateId'] . "'>" . $row['state'] . "</option>";
}
echo $options;
}
}
答案 0 :(得分:0)
多重选择会返回您需要在查询中使用in
而不是=
的数组
$states_result = $db_conx->query("select * from states where countryId IN (".implode(',',$country_id ).")";);
var selectedValues = $('#multipleSelect').val();
console.log(selectedValues)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="multipleSelect" multiple="multiple">
<option value="1" selected>Text 1</option>
<option value="2" selected>Text 2</option>
<option value="3">Text 3</option>
</select>