创建按钮(或复选框)并从中复制值以创建值列表

时间:2018-07-26 10:42:32

标签: copy-paste

我需要用电视节目,按钮或复选框上的名称制作一个简单的应用程序,当我单击多个按钮(或选择复选框)时,它将它们中的值复制到文本区域,然后可以在其他地方复制和使用。

例如,我可能有5个按钮,分别名为MTV,MTV2,FOX,CNN和ZDF。例如,当我单击其中的3个(MTV,FOX,MTV2)时,我进入以下文本区域:MTV;狐狸; MTV2(这样我就可以复制想要的电视节目列表了。)

我在HTML中找到了代码(如下),但是我不知道如何使电视节目停留在文本区域,每次我单击新的电视节目时,它只会显示该节目-不会列出该节目程序。

<html>
   <head>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script type="text/javascript">
         $(document).ready(function(){<!--  w w  w. j a  v a2  s.  c  om-->
         $("button").click(function () {
         var text = $(this).text();
         $("input").val(text);
         });
         });
      </script>
      <style>
         .selected { color:red; }
         .highlight { background:yellow; }
      </style>
   </head>
   <body>
      <div>
         <button>HRT1</button>
         <button>HRT2</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
         <button>NovaTV</button>
      </div>
      <input type="text" value="click a button" />
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

您在这里。您需要在每次单击时将文本与每一行连接起来以保存它。
                 

$(document).ready(function(){

    var textForInput= "";

$("button").click(function () {
    var text = $(this).text();

    textForInput = textForInput.concat(text + " ");
    $("input").val(textForInput);
});
});
</script>
<style>
.selected { color:red; }
.highlight { background:yellow; }
</style>
</head>
<body>
<div>
<button>HRT1</button>
<button>HRT2</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
<button>NovaTV</button>
</div>
<input type="text" value="click a button" />
</body>
</html>