我想根据文本框的值更改选择框的值我正在使用jquery自动完成在列表中显示建议,以及当我选择一个建议它成为文本框的值,现在我想根据文本框的值显示diff状态的名称。如何根据文本框的值动态更改选择框的值
我的代码:
<div class="form-group">
<label>Country*</label>
<span class="span_error"><?=$error_username;?></span>
<input name="emp_country" type="text" class="form-control" id="emp_country" placeholder="Employee Country" value="" onchange="getstate(this.value);">
</div>
<div id="countryList"></div>
</div>
我为在这里生成自动完成n的国家列表中显示的建议,这是伟大的工作。
选择框HTML:
<div class="form-group">
<label>State*</label>
<span class="span_error"><?=$error_username;?></span>
<select name="ddl_state" size="1" class="form-control" id="ddl_state">
<option value="" disabled='' selected=''>Select State</option>
</select>
</div>
我要显示在上面的文本框中给出的国家名称的差异状态名称
的Javascript:
function getstate(contry)
{
alert(contry);
$.ajax({
type:"POST",
url:"getdata.php",
data:{contry:contry},
success:function(data) {
$('#ddl_state').html(data);
}
});
}
PHP:
if (isset($_POST['contry'])) {
$contry = $_POST['contry'];
get_country_state($contry);
}
function get_country_state($contry)
{
$sql = "Select count_id from countries where count_name='$contry'LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array(result);
$country_id = $row['count_id'];
$sql_state = "select * from states where count_id='$country_id'ORDER BY state_name";
$result_state = mysql_query($sql_state);
if ($result_state) {
// code...
$output='<option value="">Select State</option>';
while ($ro = mysql_fetch_array($result_state)) {
// code...
$output .= '<option value="' . $ro["state_id"] .'">'.$ro["state_name"] . '</option>';
}
echo $output;
} else {
echo mysql_error($db);
}
}
尽管这不是一个回答你的问题,你可能想看看我对这个问题的答案:[动态下拉列表为不同的国家,州,地理位置?](http://stackoverflow.com/questions/26069761/dynamic-drop-down-list-for-different-countries-states-geographic-locations/26076664#26076664)。它包含一个功能齐全的脚本,用于实现您想要实现的功能,并且还可以安全地防止MySQL注入(因为您不是)。 – icecub
你可以请你发布你的jQuery调用,thx。您可以使用jQuery的自动完成更改事件,请参阅http://api.jqueryui.com/autocomplete/#event-change – Webomatik