Home >Backend Development >PHP Tutorial >select option 根据数据库选中,该怎么处理

select option 根据数据库选中,该怎么处理

WBOY
WBOYOriginal
2016-06-13 11:57:571011browse

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,已经测试过,但是选择框却没反应。这是什么问题啊?
------解决方案--------------------
你在浏览器中:查看源文件
------解决方案--------------------
模拟了一下,是可以的。

<br /><?php<br />$row[11] = 1;<br />?><br /><br /><select id="paid_type"><br />    <option value="0">0</option><br />    <option value="1">1</option><br />    <option value="2">2</option><br />    <option value="3">3</option><br /></select><br /><br /><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><br />


如果你还是不行,可以在循环体中输出opts[i] 与 strVal的值比对看看,是哪里出问题。
------解决方案--------------------
我的实现是这样,你参考下:
Javascript代码:
<br />case 'str':<br />                eq_select1=op=='eq'?'selected':'';<br />                eq_select2=op=='neq'?'selected':'';<br />                eq_select3=op=='include'?'selected':'';<br />                eq_select4=op=='not_include'?'selected':'';<br />            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>";<br />


测试通过。

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