问题:点击下拉框中的值,却在文本框中显示不了。
JS:
<script>
   
    function on_load()
    {
    var username=getCookie("username"); 
    var select=document.getElementsByTagName("option");
    var text=document.getElementsByTagName("input");
    if(username!=null){ 
document.getElementById('welcome').innerHTML="<input id='usrname' name='usrname' style='width:150px;height:21px'/>" + "<span style='width:18px;border:0px;'>" + "<select name='sip_uri' style='margin-left:-150px;width:168px;\' onChange='SeltxtToText(select,text)';>" + "<option>"+username+"</option>"+"</select>" + "</span>"+"<button onclick='delusr();'>删除用户名</button>";
}else{ 
var str="<input id='usrname' name='usrname' type='text' style='width:150px;height:21px'/>" + "<span style='width:18px;border:0px;'>" + "<select name='sip_uri' style='margin-left:-150px;width:168px;\'>"; 
document.getElementById('welcome').innerHTML=str; 

     
}     
   
    function on_submit()
    {
  var el=document.getElementById('usrname'); 
if(el.value){ 
setCookie("username",el.value,1); location.reload();//刷新页面 
//var n = document.form1.sip_uri.selectedIndex;
//document.form1.usrname.value = document.form1.sip_uri.option[n].text;

else 
alert("输入框不能为空");     }
    
    function getCookie(NameOfCookie)
    {
    if (document.cookie.length > 0) 
{ // 接着我们检查下cookie的名字是否存在于document.cookie // 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在 
//所以我们需要这一步看看是否有我们想要的cookie 
//如果begin的变量值得到的是-1那么说明不存在 begin = document.cookie.indexOf(NameOfCookie+"="); 
if (begin != -1)    
{ // 说明存在我们的cookie. begin += NameOfCookie.length+1;//cookie值的初始位置 
end = document.cookie.indexOf(";", begin);//结束位置 
if (end == -1) end = document.cookie.length;//没有;则end为字符串结束位置 
return unescape(document.cookie.substring(begin, end)); } 
} return null; // cookie不存在返回null 
    
    }
    
    function setCookie(NameOfCookie,value,expiredays)
    {
    var ExpireDate = new Date (); 
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); 
    document.cookie = NameOfCookie + "=" + escape(value) + 
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()); 
    
    }
    
    function delusr() 

delCookie("username"); 
location.reload(); 

    function delCookie (NameOfCookie) 

// 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间; 
//剩下就交给操作系统适当时间清理cookie啦 if (getCookie(NameOfCookie)) { 
document.cookie = NameOfCookie + "=" + 
"; expires=Thu, 01-Jan-70 00:00:01 GMT"; 

}function SeltxtToText(paSel,paTxt)
{
var   selObj,   txtObj,i;   
  selObj   =   eval(paSel);   
  txtObj   =   eval(paTxt);   
  i   =   selObj.selectedIndex;   
  if(selObj.item(i).text   ==   null){   
  txtObj.value   =   ""   
  }   else   {   
  txtObj.value   =   selObj.item(i).value;   
  }   }
    </script>
JSP:
<body onload="on_load();">
  <form name="form1">
  <div id="welcome">   
</div> 
password:<input type="password" name="password"><br>
<input type="button" name="button" value="test" onclick="on_submit();">
</form>  </body>

解决方案 »

  1.   

    <script> 
      
        function on_load() 
        { 
        var username=getCookie("username"); 
        var select=document.getElementsByTagName("option"); 
        var text=document.getElementsByTagName("input"); 
        if(username!=null){ 
    document.getElementById('welcome').innerHTML=" <input id='usrname' name='usrname' style='width:150px;height:21px'/>" + " <span style='width:18px;border:0px;'>" + " <select name='sip_uri' style='margin-left:-150px;width:168px;\' onChange=\"SeltxtToText('form1.sip_uri','form1.usrname')\";>" + "<option>&nbsp;</option><option>"+username+" </option>"+" </select>" + " </span>"+" <button onclick='delusr();'>删除用户名 </button>"; 
    }else{ 
    var str=" <input id='usrname' name='usrname' type='text' style='width:150px;height:21px'/>" + " <span style='width:18px;border:0px;'>" + " <select name='sip_uri' style='margin-left:-150px;width:168px;\'>"; 
    document.getElementById('welcome').innerHTML=str; 

        
    }     
      
        function on_submit() 
        { 
      var el=document.getElementById('usrname'); 
    if(el.value){ 
    setCookie("username",el.value,1); location.reload();//刷新页面 
    //var n = document.form1.sip_uri.selectedIndex; 
    //document.form1.usrname.value = document.form1.sip_uri.option[n].text; 

    else 
    alert("输入框不能为空");     } 
        
        function getCookie(NameOfCookie) 
        { 
        if (document.cookie.length > 0) 
    { // 接着我们检查下cookie的名字是否存在于document.cookie // 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在 
    //所以我们需要这一步看看是否有我们想要的cookie 
    //如果begin的变量值得到的是-1那么说明不存在 begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1)    
    { // 说明存在我们的cookie. begin += NameOfCookie.length+1;//cookie值的初始位置 
    end = document.cookie.indexOf(";", begin);//结束位置 
    if (end == -1) end = document.cookie.length;//没有;则end为字符串结束位置 
    return unescape(document.cookie.substring(begin, end)); } 
    } return null; // cookie不存在返回null 
        
        } 
        
        function setCookie(NameOfCookie,value,expiredays) 
        { 
        var ExpireDate = new Date (); 
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); 
        document.cookie = NameOfCookie + "=" + escape(value) + 
      ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()); 
        
        } 
        
        function delusr() 

    delCookie("username"); 
    location.reload(); 

        function delCookie (NameOfCookie) 

    // 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间; 
    //剩下就交给操作系统适当时间清理cookie啦 if (getCookie(NameOfCookie)) { 
    document.cookie = NameOfCookie + "=" + 
    "; expires=Thu, 01-Jan-70 00:00:01 GMT"; 

    } function SeltxtToText(paSel,paTxt) 

    var  selObj,  txtObj,i;  
      selObj  =  eval(paSel);  
      txtObj  =  eval(paTxt);  
      i  =  selObj.selectedIndex;  
      if(selObj.item(i).text  ==  null){  
      txtObj.value  =  ""  
      }  else  {  
      txtObj.value  =  selObj.item(i).text;  
      }  } 
        </script> 
    JSP: 
    <body onload="on_load();"> 
      <form name="form1"> 
      <div id="welcome">  
    </div> 
    password: <input type="password" name="password"> <br> 
    <input type="button" name="button" value="test" onclick="on_submit();"> 
    </form>   </body>
      

  2.   

    你的意思是不是下拉框中选择的了显示在<input id='usrname' name='usrname' type='text' style='width:150px;height:21px'/>那你就要在select 里面加onpropertychange 调用JS
    把下拉的值放进username里 
      

  3.   

    1、只有一个值的时候,默认是选择的,你再选择它,不会发生onchange事件
    2、程序设计有问题
      

  4.   

    请问<div></div>是不是不可以嵌套在table中啊?
      

  5.   

    再贴一下代码,之前按照xinyung的做是可以的,但现在又不能显示在文本框中了,但可以读到并显示cookie的值在下拉框中
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>login.jsp</title>
        <script>
    function CheckFormat(str){
        var str1 = "" + str;
        var SIPpos = str1.indexOf("sip:");
        var atpos = str1.indexOf("@");
        var ret = false;
        if(SIPpos != 0 || atpos == -1 || atpos == 4){
            ret = false;
        } else {
            ret = true;
        }
        return ret;
    }
    function on_click()
    {
    var name = document.form1.sipuri.value;
    var bret = CheckFormat(name);if(name != "")
    {
       if(bret) 
           {
    var el=document.getElementById('sipuri');
    if(el.value)
    {
    setCookie("uri",el.value,1);
    }
                   
          document.form1.submit();
           } 
       else 
           {
        window.alert("name format error!");
    form1.sipuri.focus();
           }
    }else
    {
    alert("Please input message in the text area SIP URL!!!");
    form1.sipuri.focus();
    }
    }
    function setCookie(NameOfCookie,value,expiredays) 
        { 
        var ExpireDate = new Date (); 
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); 
        document.cookie = NameOfCookie + "=" + escape(value) + 
      ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()); 
        
        } 
    function on_load()
    {
    var uri=getCookie("uri");
    if(uri != null)
    {document.getElementById('box').innerHTML="<input id='sipuri' name='sipuri' style='width:150px;height:21px'/>" + " <span style='width:18px;border:0px;'>" + " <select name='sip_uri' style='margin-left:-150px;width:168px;\' onChange='SeltxtToText('form1.sip_uri','form1.sipuri')';>" + "<option>&nbsp;</option><option>"+uri+"</option>"+" </select>" + " </span>";
    }
    else
    {
    var str=" <input id='sipuri' name='sipuri' style='width:150px;height:21px'/>" + " <span style='width:18px;border:0px;'>" + " <select name='sip_uri' style='margin-left:-150px;width:168px;\'>"; 
    document.getElementById('box').innerHTML=str;}
    }
        
    function getCookie(NameOfCookie) 
        { 
        if (document.cookie.length > 0) 

    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1)    

    begin += NameOfCookie.length+1;
    end = document.cookie.indexOf(";", begin);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(begin, end)); } 
    } return null; 

        
    function SeltxtToText(paSel,paTxt) 

    var  selObj,  txtObj,i;  
      selObj  =  eval(paSel);  
      txtObj  =  eval(paTxt);  
      i  =  selObj.selectedIndex;  
      if(selObj.item(i).text  ==  null){  
      txtObj.value  =  ""  
      }  else  {  
      txtObj.value  =  selObj.item(i).text;  
      }  } 
    </script>
        </head>
      
      <body onload="on_load();">
        <center>
    France Telecom
    <p><h2>4G Environment Platform Prototype</h2> 
    <form name="form1" method="post" action="device.xml">
    <table>
       <tr>
                <td>SIP URI:</td>
    <td><div id="box"></div></td>
                </tr>
    <tr>
    <td>Password:</td>
    <td><input type="password" name="password" style="width:168px;height:21px"></td>
    </table>
    <input type="button" name="login" value="Login" onClick="on_click();">
    </form> </center>  </body>
    </html>
      

  6.   

    高手ruyun a 我开始喜欢上这里了 呵呵