如何根据PHP和jQuery中其他下拉列表的值填充下拉列表? [英] how to populate on drop down based on the value from other drop down in php and jquery?
问题描述
我有一个下拉列表是从数据库中填充的,在同一页面上,我有第二个下拉列表,它是从第一个下拉列表的值中填充的.
I have one drop down which is populated from database and on the same page I have a seconde drop down which will be populated from the value of the 1st one.
我想为此使用ajax,所以我应该怎么做.还有可以解决问题的教程或链接.
I want to use ajax for this so what should I do for this. And tutorial or a link from where I can solve my problem.
已更新: 这是我使用的代码.
Updated: Here is the code which I used.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#cat').onChange(function(){
alert("oncha");
if($('#cat').val() == 'uprofile'){
file = 'uprofile.html';
}else if ($('#cat').val() == 'other'){
file = 'city.html';
}
$('#city').change(function(){
loadusers = $.ajax({
type: "GET",
url: file,
cache: false,
success: function (html) {
$("#city").html(html);
}
});
});
});
</script>
<tr>
<td class="label"><span id="required_span">* </span>Category</td>
<td colspan="2" class="data"><select name="cat" id="cat">
<option value="0">--Select Category--</option>
<option value="O/O">O/O</option>
<option value="company driver">company driver</option>
<option value="other">other</option>
</select></td>
</tr>
<tr>
<td class="label"><span id="required_span">* </span>City</td>
<td colspan="2" class="data"><select name="city" id="city">
<option value="0">--Select city--</option>
</select></td>
</tr>
这是city.html的内容
Here is the content of the city.html
<option>1</option>
<option>2</option>
但是即使警报功能也无法使用.我在代码中缺少什么?
But cannot work even the alert function. What I am missing in the code?
推荐答案
您可以使用类似以下代码的代码:
You can use something like this code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
if($('select').val() == 'uprofile'){
file = 'uprofile.html';
}else if ($('select').val() == 'album'){
file = 'album.html';
}
$('#dropdown').change(function(){
loadusers = $.ajax({
type: "GET",
url: file,
cache: false,
success: function (html) {
$("#dropdown2").html(html);
}
});
});
});
</script>
<body>
<div id="edit-type-wrapper">
<select id="dropdown">
<option value="">Select one</option>
<option value="albums">Album</option>
<option value="uprofile">User Profile</option>
</select>
<select id="dropdown2">
</select>
</div>
</body>
显然,您需要从其他一个或多个文件中返回数据.希望这会有所帮助
Obviously you need to return the data from the other file/files. Hope this helps
这篇关于如何根据PHP和jQuery中其他下拉列表的值填充下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!