在Javascript中将一系列输入的值添加到变量中

时间:2014-03-31 17:32:10

标签: javascript html arrays checkbox

我有一个页面,旨在通过呼叫中心的复选框向textarea添加谈话点。所有内容都需要从这一页存储和操作,因为这不会存储在服务器上,而是作为可以为每个代表定制的本地站点。我有一系列复选框,在选中时会启动一个脚本来检查复选框,并将每个选中复选框的值添加到CADI变量中。我特意选择使用textarea,以便可以实时编辑,而不是在段落块中添加。

然而,有两件事我无法弄清楚。

  1. 如何让每个复选框将自身添加到CADI变量中,而不是运行查看当前复选框的循环?
  2. 示例1:我选中了复选框2,然后手动编辑了textarea。然后,我选中了复选框3和复选框1,我需要添加它们而不重复任何复选框或删除我已经编辑的任何笔记。

    1. 是否有更有效的方法来更改复选框的.value而不单独编辑每个复选框的html代码?
    2. 代码如下所示。

      <!DOCTYPE html>
      
      <html>
      
      <head>
      
      <!-- Declare the CADI string variable so that it can hold all of our data -->
      <script>
      var CADI = "";
      </script>
      
      <!-- See which checkboxes are checked, and add those fields to CADI -->
      <script>
      function AddToCADI()
      {
      CADI = "";
      var check = document.forms[0].check;
      var txt = "";
      var i;
      for (i=0;i<check.length;i++)
        {
        if (check[i].checked)
          { 
          CADI = CADI  + " // " + check[i].value ;
          }
        }
      
      document.getElementById("wrapup").innerHTML = (CADI);
      }
      </script>
      
      <!-- Clear the CADI variable's string field and all checkboxes -->
      <script>
      function Clear()
      {
      CADI= " "
      document.getElementById("wrapup").innerHTML = (CADI);
      var check = document.forms[0].check;
      for (i=0;i<check.length;i++)
      {
      check[i].checked=false;
      }
      }
      </script>
      
      
      <!-- Copy Text to Clipboard -->
      <script>
      function ClipBoard()
      {
      Copied = document.getElementById("wrapup").createTextRange()
      Copied.execCommand("Copy");
      }
      </script>
      
      </head>
      
      
      <body>
      
      <table width="700">
      
      <tr>
      <td colspan="3" style="background-color:#1E90FF";>
      
      <h1>Click a button to add the it to the Wrap Up!</h1>
      
      <td>
      </tr>
      
      
      <tr>
      <td style="width:300px;height:500px;border:1px solid black;">
      
      <b></b>
      
      <form action="">
      
      <!-- Checkboxes to add  to the CADI variable -->
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      
      
      <br>
      
      
      
      
      
      </td>
      
      <td id="quick" style="width:400px;height:500px;border:1px solid black;">
      
      
      
      <!-- Checkboxes to add  Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <br>
      
      <!-- Checkboxes to add  Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <br>
      
      <!-- Checkboxes to add  Form Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <br>
      
      <!-- Checkboxes to add  Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <br>
      
      <!-- Checkboxes to add Written Appeal Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <br>
      
      <!-- Checkboxes to add  Items to the CADI variable -->
      <b></b><br>
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      
      <br>
      </td>
      <td id="quick" style="width:300px;height:500px;border:1px solid black;">
      
      <b>Miscellaneous</b><br>
      
      <!-- Checkboxes to add Miscellaneous  to the CADI variable -->
      
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      <input type="checkbox" onclick="AddToCADI()" name="check" value=""><br>
      
      <p> </p>
      
      <!-- Button to copy text to clipboard -->
      <button onclick="ClipBoard()">Copy</button>
      
      <br>
      <br>
      
      
      <!-- Button to clear the CADI variable -->
      <button onclick="Clear()">Clear All Notes</button>
      <br>
      
      </form>
      
      
      
      </td>
      </tr>
      
      <tr>
      <td colspan="3" style="width:900px;text-align:center;">
      
      <!-- Field in which wrapup items will appear -->
      <textarea id=wrapup rows="50" cols="100">
      This is where your CADI is going to show up.
      </textarea>
      
      <!-- Field that will hold the text so it can be copied to the clipboard -->
      <textarea id="holdtext" style="display:none;">
      </textarea>
      
      </td>
      </tr>
      
      </table>
      
      </body>
      </html>
      

0 个答案:

没有答案