如何使Jquery编码的动态下拉菜单与PHP GET变量协调工作
How to make Jquery coded Dynamic Dropdown Menus work in harmony with PHP GET variables
我有一个用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>
相关文章:
- 如何将PHP get查询转换为Meteor's HTTP.get()
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- 如何使Jquery编码的动态下拉菜单与PHP GET变量协调工作
- 如何使用jQueryPOST将get变量从JavaScript传递到PHP-Mysql
- PHP $_GET with jQuery $.get call
- Javascript 变量通过 AJAX(GET 方法)到 PHP
- 如何使用 GET 和 POST 语句而不使用 PHP/Laravel 重新加载页面
- Javascript POST数据进入GET PHP数组
- AJAX请求php获取post/get发送的指定目录中服务器上的文件列表(无JQUERY)
- Get latlng from different addresses using PHP & JavaScri
- 如何使用Javascript将GET和FILE传递到PHP文件
- Javascript等同于PHP的GET变量
- 为什么我的PHP函数没有由jQuery $.get执行
- 使用get方法在同一页面上的PHP中获取文本框Vlaue
- 带有PHP(未定义索引)的Youtube API V3 GET横幅图像
- PHP 会话变量在第一页的值在第二个 GET 请求后未更新为第二页会话变量的值
- 如何打开一个“get”?PHP窗口从jquery没有父页面刷新
- 我想加载一个get.php文件在iframe上的index.html一旦提交按钮被按下
- AngularJS $http.get php file - $injector:modulerr
- $.get PHP 文件在 JS 中单击链接时