对于不允许使用开源标签进行select选中的js替代方法

来源:互联网 发布:数据冗余类型 编辑:程序博客网 时间:2024/04/16 17:03

对于项目中不允许使用jstl等标签进行判断html中select标签的选中问题,可以使用js代替,具体如下

 <td >作业分类:</td>
    <td ><select name="taskCode" id="taskCode">
    <option value="" selected="selected">所有分类</option>
      <oms:iterate items="project">
     <option value='<oms:itevalue property="codingCodeId" />'><oms:itevalue property="codingName" /></option>
    </oms:iterate>
        </select>
        <script type="text/javascript">
         var taskCode = document.getElementById('taskCode');
         var select = '<oms:string property="taskCode" defaultValue=""/>';
         var opt = taskCode.options;
         for(var i=0;i<opt.length;i++){
          if(select == opt[i].value){
           opt[i].selected = true;
          }
         }
        </script>
        </td>

说明:其中标红的js部分为判断选中所有使用的js代码。 taskCode为select标签,select为需要被选中的option的value

对于select选中option的value的第二种获取方式如下:

<script>

 var select;

var selectValue = select.options[select.selectedIndex].value;

</script>