当onchange事件发生在select选项上时,textarea值不会更新

时间:2013-07-08 23:00:49

标签: javascript html5 textarea onchange

我想为什么当用户从下拉菜单中选择时,我的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>

2 个答案:

答案 0 :(得分:1)

就像Musa所说,JavaScript区分大小写,因此document.changeform.changeType;应为document.changeform.ChangeType;

然而,仅凭它不会起作用。

在你updateDescImpact()的第二行:

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

您试图将该值解析为整数,然后尝试将其与下一行的sting进行比较。

摆脱数据转换,你不应该有任何问题:

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

答案 1 :(得分:0)

我不知道您的代码有什么问题,以下工作正常。我做了一些小改动:

  1. 删除脚本元素中无用的HTML注释分隔符
  2. 传递对侦听器中的select的引用
  3. 更有效的表单访问
  4. 更高效的select element vlaue access
  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>