2013-07-08 18 views
0

我想爲什麼我的textarea的值在用戶從下拉菜單中選擇時沒有更新?我使用條件字符串值不正確?當onchange事件發生在select選項上時,textarea值不會更新

這是我想要發生的事情;用戶選擇Filer Changes選項我想用默認值填充textarea,和/或允許用戶更新值,並將其發佈到php頁面。

<html><head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <title></title> 
    <script type="text/javascript"><!-- 
     function updateDescImpact() { 
     var changeSel = document.changeform.changeType; 
     var changeType = parseInt(changeSel.options[changeSel.selectedIndex].value); 
     if(changeType == "Filer Changes") { 
      document.changeform.description.value = "This is the Filer Change Description"; 
      document.changeform.impact.value = "This is the Filer Changes impact statement"; 
     } else if(changeType == "DNS Changes") { 
      document.changeform.description.value = "This is the DNS Change Description"; 
      document.changeform.impact.value = "This is the DNS Changes impact statement"; 
     } else { 
      document.changeform.description.value = ""; 
      document.changeform.impact.value = ""; 
     } 
    }   

    // --> 
    </script> 

    </head> 
    <body> 
    <form name="changeform" method="post" action=""> 
    <table width="100%" border="0" cellspacing="1" cellpadding="1"> 
    <tr> 
    <td width="18%">Change Type</td> 
    <td width="82%"><select name="ChangeType" id="ChangeType" onchange="updateDescImpact()"> 
    <option value="Filer Changes">Filer Changes</option> 
    <option value="DNS Changes">DNS Changes</option> 
    </select></td> 
    </tr> 
    <tr> 
    <td>Description</td> 
    <td> 
    <textarea name="description" id="description" cols="50" rows="10"> This needs to be updated</textarea></td> 
    </tr> 
    <tr> 

    <td>Impact</td> 
    <td> 

     <textarea name="impact" id="impact" cols="50" rows="10">This needs to be updated</textarea> 
     </td> 
    </tr> 
    </table> 
    </form> 
    </body> 
    </html> 

回答

1

就像穆薩說,JavaScript是區分大小寫,因此document.changeform.changeType;應該document.changeform.ChangeType;

然而,僅僅將不會使它工作。

在第二行

updateDescImpact()你有:

var changeType = parseInt(changeSel.options[changeSel.selectedIndex].value);

您正試圖解析值爲整數,但隨後試圖把它比作下一行有刺。

擺脫數據轉換的,你不應該有任何問題:

var changeType = changeSel.options[changeSel.selectedIndex].value;

+0

我覺得這是個問題,'parseInt函數( '文件管理器更改')''給NaN',這不等於什麼。 – RobG

+1

非常感謝穆薩和本傑明的主要問題是我試圖解析changeSel值作爲一個整數。 –

+0

很高興幫助!如果您會如此友善,請選擇一個答案並關閉該問題? – BenJamin

0

我不知道什麼是你的代碼錯誤,下面的工作正常。我做了一些細微的變化:

  1. 刪除無用的HTML腳本元素
  2. 註釋分隔符從聽者
  3. 更有效的形式訪問
  4. 更有效的選擇元素vlaue傳遞到選擇參考訪問
  5. 添加一個提交按鈕

    function updateDescImpact(el) { 
        var form = el.form; 
        var changeType = el.value; 
    
        if (changeType == "Filer Changes") { 
         form.description.value = "This is the Filer Change Description"; 
         form.impact.value = "This is the Filer Changes impact statement"; 
    
        } else if (changeType == "DNS Changes") { 
         form.description.value = "This is the DNS Change Description"; 
         form.impact.value = "This is the DNS Changes impact statement"; 
    
        } else { 
         form.description.value = ""; 
         form.impact.value = ""; 
        } 
        }   
    
    </script> 
    

    <form name="changeform" method="post" action=""> 
        <table width="100%" border="0" cellspacing="1" cellpadding="1"> 
        <tr> 
         <td width="18%">Change Type</td> 
         <td width="82%"><select name="ChangeType" id="ChangeType" onchange="updateDescImpact(this)"> 
         <option value="Filer Changes">Filer Changes</option> 
         <option value="DNS Changes">DNS Changes</option> 
         </select></td> 
        </tr> 
         <tr> 
         <td>Description</td> 
         <td> 
         <textarea name="description" id="description" cols="50" rows="10"> This needs to be updated</textarea></td> 
        </tr> 
        <tr> 
         <td>Impact</td> 
         <td><textarea name="impact" id="impact" cols="50" rows="10">This needs to be updated</textarea></td> 
        </tr> 
        <tr> 
         <td colspan="2"><input type="submit"> 
        </table> 
    </form> 
    

+0

我原來有一個提交按鈕,問題是我試圖解析一個整型'Var changeType = parseInt(changeSel.options [changeSel.selectedIndex] .value);'我已經移除了parseInt並解決了問題 –

相關問題