用js分别获取select的value 和 text 文本

来源:互联网 发布:本色服饰淘宝旗舰店 编辑:程序博客网 时间:2024/05/08 08:36

 <td nowrap="nowrap">
            <div align="center">
             环节:
            </div>
           </td>
           <td>
            <html:select name="departmentDeclareForm" property="cycleId">
             <html:options collection="cycleList" property="value"
              labelProperty="name" />
            </html:select>
            <input type="hidden" name="oldCycleId"/>
           </td>

1.获取 cycleId 的value 即 id值

newRow.cells(1).innerText =document.forms[0].elements("cycleId").value;//环节

2.获取 cycleId 的text 即静态文本

newRow.cells(1).innerText =document.forms[0].elements("cycleId").options[document.forms[0].elements("cycleId").selectedIndex].text;//环节

或者:

var ops = document.forms[0].elements("cycleId").options;
       var idx = document.forms[0].elements("cycleId").selectedIndex;
       alert(ops[idx].text);

 

下面是从网上找到的一个方法

Script   language= "javascript ">

function   query()   {
alert(form1.select.options[form1.select.selectedIndex].text);
      if(form1.select.options[form1.select.selectedIndex].text== "计算机 "){
            return   form1.select.options[form1.select.selectedIndex].text;
    }else   if(form1.select.options[form1.select.selectedIndex].text== "机械系 "){
      return   form1.select.options[form1.select.selectedIndex].text;
      }else   if(form1.select.options[form1.select.selectedIndex].text== "电子系 "){
      return   form1.select.options[form1.select.selectedIndex].text;
      }else   if(form1.select.options[form1.select.selectedIndex].text== "数学系 "){
      return   form1.select.options[form1.select.selectedIndex].text;
      }
}

</script>
<form   name= "form1 ">    
<select   name= "select ">
                <option   value= "0 "> 请选择 </option>
<option> 机械系 </option>
<option> 电子系 </option>
<option> 数学系 </option>
<option> 计算机 </option>
</select>  
<input   type= "button "   value= "确定 "   onclick= "query() ">
</form>