如何使Jquery编码的动态下拉菜单与PHP GET变量协调工作

How to make Jquery coded Dynamic Dropdown Menus work in harmony with PHP GET variables

本文关键字:GET PHP 变量 协调 工作 下拉菜单 Jquery 何使 编码 动态      更新时间:2023-09-26

我有一个用jquery和php编码的3种游戏类型的动态下拉菜单。对于"游戏类型"下拉列表,如果选择了"棒球",则相应的联赛将仅显示"棒球联赛",如果选择"足球",则对应的联赛将只显示"足球联赛",依此类推。这是使用jquery实现的。我被迫使用$("#game_type_id").change();从而对于所选择的任何游戏类型都将显示相应的联赛。但缺点是,无论你在League Dropdown上选择什么,在页面提交后,由于强制执行$("#game_type_id").change(),选择永远不会保留。但如果我们要检查选择是否正确,我可以通过php链接说是的,比如game_typeid=1&q_lid=64,则发生变化。

有没有一种方法可以使它与PHP代码协调一致?

HTML(Smarty PHP Framework)

Game Type:
<select id="game_type_id" name="game_type_id" style="width:100px;margin-bottom:5px;  margin-top: 10px;margin-left: 5px;">
 <option value="1" {if $game_type_id == 1}selected="selected" {/if}> Baseball</option>
 <option value="2" {if $game_type_id == 2}selected="selected" {/if}> Soccer </option>
 <option value="3" {if $game_type_id == 3}selected="selected" {/if}> Flexi </option>
</select>
League:
<select name="q_lid" id="q_lid">
 <option value="0">-- Select --</option>
  {foreach from=$league_list item=league}
  <option class="league_group league_group_{$league.game_type_id}" value="{$league.league_id}" {if $q_lid==$league.league_id} selected="selected" {/if}>{$league.league_short_name}</option>
  {/foreach}
</select>
<select id="fl_ctg" name="fl_ctg">
 <option value="0" > --Flex Category -- </option>
 {foreach $f_bet_type as $val}
 <option value="{$val.category_id}" {if $fl_ctg == $val.category_id}selected="selected"{/if}>{$val.category_full_name}</option>
 {/foreach}
</select>
<input type="submit" id="btn_search" name="btn_search" value="Search" style="cursor:pointer;width:50px;"/>

Js代码

$(document).ready(function(){
 $(function () {
    $("#game_type_id").change();
 });
 var options = $("#q_lid").children(".league_group").clone();
 //Game Type Dropdown
 $("#game_type_id").bind("change",function() {
    var type = $(this).val();
    $("#q_lid").prop("disabled",false);
    if(type == 3)
        $("#fl_ctg").show();
    else
    {
        $("#fl_ctg").hide();
        $("#fl_ctg").val(0);
    }
    // select league according to game type
    $(".league_group").prop('selected', false)
        .remove();
    options.clone()
        .filter('.league_group_'+this.value)
        .appendTo('#q_lid')
        .first()
        .prop('selected', true);
    $("#q_lid").change();
});
//League Dropdown
$("#q_lid").bind("change",function() {
    var gtype = $("#game_type_id").val();
    if(gtype==3)
        $("#fl_ctg").show();
    else
    {
        $("#fl_ctg").hide();
        $("#fl_ctg").val(0);
    }
});
$('#btn_search').click(function()
{
    var q_lid = $('#q_lid').val();
    var fl_ctg = $('#fl_ctg').val();
    var q_str = '';
    if(q_lid.length > 0){
        q_str += '&q_lid='+q_lid;
    }
    if(fl_ctg.length > 0){
        q_str += '&fl_ctg='+fl_ctg;
    }
    if(game_type_id.length > 0){
        q_str += '&game_type_id='+game_type_id;
    }
});

PHP代码

if( isset($_GET['q_lid']) && !empty($_GET['q_lid']) ){
  $q_lid = mysql_real_escape_string(trim($_GET['q_lid']));
}
if(isset($_GET['fl_ctg']) && $_GET['fl_ctg']<>0)
{
    $fl_ctg = mysql_real_escape_string(trim($_GET['fl_ctg']));
}
$smarty->assign('q_lid',$q_lid);
$smarty->assign('fl_ctg',$fl_ctg);
$sql_team_orderby = "league_priority DESC";
$league_data = LeagueClass::getLeagueDetails('', '', '',$sql_team_orderby, '');
$smarty->assign('league_list', $league_data);
$f_sql_category="SELECT * FROM category ";
$f_category_result=Db::getInstance()->queryExecuteFull($f_sql_category, $array = true);
$smarty->assign('f_bet_type', $f_category_result);

您可以只使用PHP 动态地执行此操作

<?php
    $game_type_id = $_GET['game_type_id']; // Game ID
    $league_list = array('league1', 'league2', 'league3'); // The list of all leagues
    $chosen_league = $_GET['league']; // whatever the league is
?>

游戏类型>棒球>足球>Flexi

联盟

<select name="q_lid" id="q_lid">
    <option <?php if ($chosen_league == '') echo 'selected' ?> value="" disabled>Select...</option>
    <?php
        // Output all the league list with the chosen one
        foreach($league_list as $option) {
            printf(
                "<option %s value='%s'>%s</option>'n",
                $chosen_league == $option ? 'selected' : '',
                $option,  $option
            );
        }
    ?>
</select>