当前位置:编程学习 > JS >>

js 怎么获取 select 下拉框选中的值

用js 怎么获取 动态添加行的 select 当前选中的文本框里的值

高手帮我看下:代码如下(请复制到自己电脑上运行)

现在的代码只可以获取第一个默认的值,选择其他的,不行。

<html>
<head>
  <title> New Document </title>
    <script type="text/javascript">

      var tab;
      var index;
      function TableFunction()
        {
      
                tab = document.getElementById("tabMain");
                var newRow = tab.insertRow(tab.rows.length);
                newRow.setAttribute('onmouseout','RowOnMouseout(this)');
                newRow.setAttribute('onmousemove','RowOnMouseover(this)');
                newRow.setAttribute('style','text-align:center;');
               
                var  cell1=newRow.insertCell(-1);
                cell1.setAttribute('style','text-align:center;');
                newRow.insertCell(-1).innerHTML = '<input onchange="GetHeadgearById(this)"  style="width: 100px;border:0px;height:18px;text-align:center;" type="text" value="" />';//首饰编号
                newRow.insertCell(-1).innerHTML = '<input  onchange="SelectGoods(this)" style="width: 150px;border:0px;height:18px;text-align:center;" type="text" value="" />'   //首饰名称
                newRow.insertCell(-1).innerHTML='<input type="hidden" id="hntype" />' ;//类型
                newRow.insertCell(-1).innerHTML='<input onchange="Sum(this)"  style="width: 50px;border:0px;height:18px;text-align:center;" type="text" value="0"   />';  //数量

                newRow.insertCell(-1).innerHTML='<input  style="width: 100px;border:0px;height:18px;text-align:center;" ondblclick="showSales(this)" type="text"  /><input  type="hidden" id="yyyid" />'; //营业员
                newRow.insertCell(-1).innerHTML='<input  style="width: 100px;border:0px;height:18px;text-align:center;" type="text" value="0"  />'; //积分

                newRow.insertCell(-1).innerHTML='<select  style="width: 60px;" name="cl_guitai" ><option> 默认</option><option>张三</option> <option>李四</option> </select>';
                //newRow.insertCell(-1).innerHTML='<input  style="width: 100px;border:0px;height:18px;text-align:center;" ondblclick="showCounter(this)" type="text"  />';   //柜台
                newRow.insertCell(-1).innerHTML='<select id="celType" style="width: 50px;"  onchange="Sum(this)"> <option> </option><option value="Q">数量</option> <option value="W">重量</option></select>';  //公费计价
                newRow.insertCell(-1).innerHTML='<input type="text" style="width: 100px;border:0px;height:18px;text-align:center;" value=" "  />';  //摘要
                //newRow.insertCell(-1).innerHTML= '<input  type="hidden" value="" />';  //单位id
               
                CreateLine();
        }
       function del()
       {
            tab = document.getElementById("tabMain");
            if(tab.rows.length>1)
            {
           
                if(confirm("是否真的删除一行!"))
                {
                    tab.deleteRow(tab.rows.length-1);
                 CreateLine();
                }
            }
       }
        //创建连续的行号
        function CreateLine()
        {
            tab =document.getElementById("tabMain");
            for(var i=1;i<tab.rows.length;i++)
            {
                tab.rows[i].cells[0].innerHTML = i;
                if(i % 2==0)
                {
                    //tab.rows[i].style.backgroundColor="#DBDAD9";
                }
            }
        }
       
        //获取 select 下拉列表框  选中的值
        function test()
        {
             var tab=document.getElementById("tabMain");
            var text="";
            for(var i=1;i<tab.rows.length;i++)
            {
            
                text =tab.rows[i].cells[7].childNodes[0].options[0].text;  // c_counter  ,      柜台名称
               alert(text);
               
            }
        }
    </script>
</head>

<body >
 <input type="button"  onclick="TableFunction()" value="加一行" /> <input type="button"  onclick="del()" value="减一行" />
  <table id="tabMain" style="margin-left:5px;margin-top:5px;" width="800px" border="1" cellpadding="0" cellspacing="0">
        <tr   style="text-align:center;">
            <td>(行号)</td>
            <td>首饰编号</td>
            <td>首饰名称</td>
            <td>类型</td>
            <td>数量</td>
            <td>营业员</td>
            <td>积分</td>
            <td>柜台</td>
            <td>工费计价</td>
            <td>摘要</td>
        </tr>
    </table>
   <input id="Button1"  onclick="test()" type="button" value=" 获取柜台选中的值 " />
                    
  </body>
</html>

 

怎么获取呢?

追问:辛苦啦,我这里 是动态 添加的没有 id
答案:用js获取select所选的值 用js获取select所选的值 2009-09-02 10:04 
<script type="text/javascript">
   function change(obj){
    var strsel = obj.options[obj.selectedIndex].text;
    document.getElementById("xiangmutext").value=strsel;
   
   }
</script>
投票项目 :
<html:select property="xiangmu" onchange="change(this)">
或:<html:select property="xiangmu" onchange="change('xiangmu')">
      <html:option value="1" >姚明</html:option>
    <html:option value="2">买努</html:option>
    <html:option value="3">科比</html:option>
    </html:select>
    <html:text property="xiangmutext" value="姚明"/>
text中就显示了select所选的内容
你这个是用的jsp吧?  你应该会简单的js去提取下拉列表的值吧? 你先把项目部署,然后打开jsp页面,查看源代码。看jsp代码翻译成源代码是什么样子的,然后修改jsp里面代码,提取就OK了

上一个:如何用js动态显示本地时间?
下一个:这段js怎么还是未定义呢?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,