0

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

2 回答 2

1

就像 Musa 说的,JavaScript 是区分大小写的,所以document.changeform.changeType;应该是document.changeform.ChangeType;

然而,仅凭这一点并不能让它发挥作用。

updateDescImpact()你的第二行:

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

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

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

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

于 2013-07-08T23:22:33.143 回答
0

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

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

于 2013-07-08T23:57:00.230 回答