简体   繁体   English

从选定选项中检索值到另一个选定项

[英]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.

相关问题 如何从多个选择选项中获取所有选择的值? - How to get all selected values from multiple select option? 如何根据选择选项中的选定值列出值 - How to list the values according to selected value from select option 如果选择了选项,则在另一个选项中显示值 - If option is selected show values in another option 当从另一个 select 中选择了特定选项时,隐藏多个 select 中的选项 - Hide an option in a multiple select, when a specific option has been selected from another select 从选择菜单中的选定选项中获取值,然后在相似的选定菜单中重复这些值Java Script,PHP - Getting values from selected option in select menu and repeat them in similar selected menus Java Script, PHP 用户选择的OPTION值的MYSQL SELECT - MYSQL SELECT with user selected OPTION values 从数据库中检索在表单上选择的多个值 - Retrieving multiple values selected on a form from a database 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 选择未选中的选项 - Select option selected an not selected 使用多个选择选项在下拉列表中检索SQL数据库值并发布值 - Retrieving SQL database value in dropdown with multiple select option and post the values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM