Home >Backend Development >PHP Tutorial >select option 根据数据库选中

select option 根据数据库选中

WBOY
WBOYOriginal
2016-06-23 13:56:40927browse

我写了以下代码:
<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>


如果你还是不行,可以在循环体中输出opts[i] 与 strVal的值比对看看,是哪里出问题。

我的实现是这样,你参考下:
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,看下是否报错?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn