php中select下拉选框默认项的动态设置

<?phpheader("content-type:text/html;charset=gbk");$studenttype="博士";echo($studenttype... <?php
header("content-type:text/html;charset=gbk");
$studenttype="博士";
echo ($studenttype=='硕士')?'selected':'';
echo ($studenttype=='博士')?'selected':'unselected';
echo ($studenttype=='留学生')?'selected':'unselected' ;

echo "<td><select name='studenttype'>";
echo "<option value='硕士' ($studenttype=='硕士')?'selected':' ' > 硕士</option> ";
echo "<option value='博士' ($studenttype=='博士')?'selected':' ' > 博士</option> ";
echo "<option value='留学生' ($studenttype=='留学生')?'selected':' ' > 留学生</option> ";
echo "</select> </td>";
?>
我的目的很简单,就是根据$studenttype值的不同,使select下拉菜单的默认选项不同。但是我这段代码结果中下拉选项始终都是第一项“硕士”为默认的,我很郁闷,但是中间echo 的那三行还是正确的比较结果啊,分别输出unselected、selected、unselected。我是想让“博士”为默认的啊,哪位大侠指点一二.....
展开
 我来答
feng663727
2010-12-03 · TA获得超过263个赞
知道小有建树答主
回答量:108
采纳率:0%
帮助的人:148万
展开全部
示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS Selector</title>
<script type="text/javascript">
function createSelect(c){
var _inner = {
"nation" : ['汉族','蒙古族','彝族','侗族','哈萨克族',
'畲族','纳西族','仫佬族','仡佬族','怒族','保安族',
'鄂伦春族','回族','壮族','瑶族','傣族','高山族',
'景颇族','羌族','锡伯族','乌孜别克族','裕固族','赫哲族',
'藏族','布依族','白族','黎族','拉祜族','柯尔克孜族','布朗族',
'阿昌族','俄罗斯族','京族','门巴族','维吾尔族','朝鲜族',
'土家族','僳僳族','水族','土族','撒拉族','普米族','鄂温克族',
'塔塔尔族','珞巴族','苗族','满族','哈尼族','佤族','东乡族',
'达斡尔族','毛南族','塔吉克族','德昂族','独龙族','基诺族'],
"shengxiao" : ['鼠','牛','虎','兔','蛇','蛇','马','羊','猴','鸡','狗','猪'],
"degree" : ['小学','初中','高中','中专','大专','本科','硕士','博士']
}
var _array = c["array"] || _inner[c["type"]];
var _select = document.createElement("select");
for(var i=0; i < _array.length; i++){
_select.options[i] = new Option(_array[i], _array[i]);
_array[i] == c["selected"] && (_select.options[i].selected = true);
}
c["id"] && (_select.id = c["id"]);
c["name"] && (_select.name = c["name"]);
c["onchange"] && (_select.onchange = c["onchange"]);
return _select;
}

function loadRender(){
document.getElementById('field-nation').appendChild(
createSelect({type : "nation", selected : "汉族", name : "nation"})
);
document.getElementById('field-education').appendChild(
createSelect({type : "degree", selected : "本科", name : "education"})
);
}

</script>
</head>

<body onload="loadRender();">
<div id="field-nation"></div>
<div id="field-education"></div>
</body>
</html>

直接把变量传到:
document.getElementById('field-nation').appendChild(
createSelect({type : "nation", selected : "<?=$nation?>", name : "nation"})
);
document.getElementById('field-education').appendChild(
createSelect({type : "degree", selected : "<?=$degree?>", name : "education"})
);
lhhxxgl
2010-12-03 · TA获得超过1028个赞
知道小有建树答主
回答量:364
采纳率:0%
帮助的人:273万
展开全部
$stype = $_GET['studenttype']=='' ? '博士':$_GET['studenttype'];
$studenttype = array($stype=>' selected');

echo "<option value='硕士' ".$studenttype['硕士']." > 硕士</option> ";
echo "<option value='博士' ".$studenttype['博士']." > 博士</option> ";
echo "<option value='留学生' ".$studenttype['留学生']." > 留学生</option> ";

这样就可以了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
晨阳的知道
2012-05-09 · 贡献了超过180个回答
知道答主
回答量:180
采纳率:0%
帮助的人:27.2万
展开全部
求完整代码,我怎么也实现不了。
急急急!
谢谢!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xeonol
推荐于2018-04-11 · TA获得超过870个赞
知道小有建树答主
回答量:528
采纳率:0%
帮助的人:679万
展开全部
改成字符串拼接:
echo "<option value='硕士' ".($studenttype=='硕士')?'selected':' '." > 硕士</option> ";
echo "<option value='博士' ".($studenttype=='博士')?'selected':' '." > 博士</option> ";
echo "<option value='留学生' ".($studenttype=='留学生')?'selected':' '." > 留学生</option> ";
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式