[英]retrieving values from selected option to another select
I'm trying to write this code to retrieve data from the first Select option to the second Select: 我正在尝试编写此代码,以从第一个Select选项检索数据到第二个Select:
Fist Select: 拳头选择:
<select name="league">
<option value="0">------------Please Select League-------</option>
<?php
$sql_league="select * from ".$prev."league";
$re_league=mysql_query($sql_league);
while($d_league=mysql_fetch_array($re_league))
{
?>
<option value="<?=$d_league['id']?>" <?php if($_SESSION['id']==$d_league['id']){?> selected="selected" <?php }?>><?=$d_league['title']?></option>
<?php
}
?>
</select>
Second Select: 第二选择:
<select name="team">
<option value="0">------------Please Select team-------</option>
<?php
$sql_team="select * from ".$prev."team where leagueID=".$d_league['id']."";
$re_team=mysql_query($sql_team);
while($d_team=mysql_fetch_array($re_team))
{
?>
<option value="<?=$d_team['id']?>" <?php if($_SESSION['id']==$d_team['id']){?> selected="selected" <?php }?>><?=$d_team['title']?></option>
<?php
}
?>
</select>
Second Select should depend on the selection from the first Select (If I chose League1, the Second select options should show teams assigned to that league). 第二个选择项应取决于第一个选择项的选择(如果我选择了League1,则第二个选择项应显示分配给该联赛的球队)。
any Ideas how to make this work? 任何想法如何使这项工作?
try this 尝试这个
<select name="league" onChange="bindSecondSelect()">
<option value="0">select</option>
// your php code
</select>
<select name="team">
// your php code
</select>
<script type="text/javascript" src="jquery-1.7.js"></script>
<script type="text/javascript">
function bindSecondSelect()
{
if($('[name="league"]').val() != "0")
{
$('[name="team"]').find('option').css('display','none');
$('[name="team"]').find('option[value="'+$('[name="league"]').val()+'"]').css('display','block');
}
}
</script>
Send an ajax request to a function with the first select value. 使用第一个选择值向函数发送ajax请求。 This function should get the details of that value and returns the details to the browser.
此函数应获取该值的详细信息,并将详细信息返回给浏览器。 Use that details and append it into the second select.
使用该详细信息并将其附加到第二选择中。
I have created a little fiddle for you http://jsfiddle.net/TqKG3/3/ . 我为您创建了一个小提琴http://jsfiddle.net/TqKG3/3/ 。
I have used some default values to demonstrate, you will have to use ajax call and return json. 我已经使用一些默认值进行了演示,您将不得不使用ajax调用并返回json。
For eg : 例如:
$("#opt_leg").change(function () {
$("#opt_mem").find('option').remove();
var url = "your_url.php?league_id=" + $(this).val();
$.get(url, function(data){
var list = jQuery.parseJSON(data);
$.each(list, function (i, item) {
$("#opt_mem").append("<option value=\"" + item.id + "\">" + item.name + "</option>");
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.