Heim >Backend-Entwicklung >PHP-Tutorial >select option 根据数据库选中
我写了以下代码:
<script> <br /> function setSelectByValue(domSelect,strVal){ <br /> var opts = domSelect.options; <br /> for(var i=0,j=opts.length;i<j;i++){ <br /> if(opts[i].value===strVal){ <br /> return domSelect.selectedIndex=i; <br /> } <br /> } <br /> } <br /> setSelectByValue(document.getElementById('paid_type'),'1‘); <br /> </script>
这样能正确显示选择值为1的选项,然后我把数据库查询到的值代进去如下:
<script> <br /> function setSelectByValue(domSelect,strVal){ <br /> var opts = domSelect.options; <br /> for(var i=0,j=opts.length;i<j;i++){ <br /> if(opts[i].value===strVal){ <br /> return domSelect.selectedIndex=i; <br /> } <br /> } <br /> } <br /> setSelectByValue(document.getElementById('paid_type'),'<?php echo ($row[11])?>'); <br /> </script>
输出是1,已经测试过,但是选择框却没反应。这是什么问题啊?
你在浏览器中:查看源文件
模拟了一下,是可以的。
<?php$row[11] = 1;?><select id="paid_type"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option></select><script>function setSelectByValue(domSelect,strVal){ var opts = domSelect.options; for(var i=0,j=opts.length;i<j;i++){ if(opts[i].value===strVal){ return domSelect.selectedIndex=i; }}}setSelectByValue(document.getElementById('paid_type'),'<?php echo ($row[11])?>');</script>
我的实现是这样,你参考下:
Javascript代码:
case 'str': eq_select1=op=='eq'?'selected':''; eq_select2=op=='neq'?'selected':''; eq_select3=op=='include'?'selected':''; eq_select4=op=='not_include'?'selected':''; result="<select name='op'><option value='eq' "+eq_select1+">等于</option><option value='neq' "+eq_select2+">不等于</option><option value='include' "+eq_select3+">包含</option><option value='not_include' "+eq_select4+">不包含</option></select>";
通过浏览器firebug,看下是否报错?