【问题标题】:html table with radio button copy paste into xls带有单选按钮的html表复制粘贴到xls
【发布时间】:2012-08-28 19:41:00
【问题描述】:

我有一个 HTML table 元素,其中的行不仅包含纯文本字符,还包含 radio buttonslists 等。

我想要的是,当用户将表格的内容复制粘贴到 MS Excel 中(作为纯文本)时,radio buttons 应该替换为它们的值(例如:“checked”和“未选中”),列表元素应替换为其选择的值等。

有没有办法在浏览器中实现这个? (最好不使用flash或java小程序组件)

谢谢, 克丽丝

【问题讨论】:

    标签: javascript html excel


    【解决方案1】:

    您可以使用 javascript(使用 jquery 之类的框架会更容易)来修改已复制的代码。一般过程是将单选按钮设置为不可见,然后根据它们的当前值在其位置添加文本。

    有很多问题可以帮助解决实施细节:https://stackoverflow.com/search?q=%5Bjavascript%5D+copied+text&submit=search

    更新: 事实证明,您甚至不需要删除单选按钮,至少对于我的 excel 版本而言。这是一个工作示例(过度评论以解释发生了什么):

    <!DOCTYPE html>
    <html>
      <head>
        <style type='text/css'>
          .radioCopyVal{
          font-size: 0;
          }
        </style>
      </head>
      <body>
        <form>
          <table>
        <tr>
          <td><input type='radio' name='woots' id='oneWoot' checked/><span class='radioCopyVal'>Checked.</span></td>
          <td><label for='oneWoot'>Woot.</label></td>
        </tr>
        <tr>
          <td><input type='radio' name='woots' id='twoWoot' /><span class='radioCopyVal'>Not checked.</span></td>
          <td><label for='twoWoot'>Woot. Woot.</label></td>
        </tr>
          </table>
        </form>
        <script src="jquery-1.7.2.min.js"></script>
        <script type='text/javascript'>
          $("#oneWoot").attr("checked", true); //on page load, make sure oneWoot is the default selection
    
          $("input[name=woots]:radio").change(function(){ //anytime the radio buttons of name woots are changed...
              $(".radioCopyVal").remove(); //remove the old radioCopyVal spans (to be replaced with new ones)
              $("input[name=woots]:radio").each(function() { //for each radio button in name woots
                  if ($(this).attr("checked")) { //if the button is checked, add a span saying 'Checked.' after it. css is used to hide the span
                      $(this).after('<span class="radioCopyVal">Checked.</span>');
              }else {
                  $(this).after('<span class="radioCopyVal">Not checked.</span>');
              }
              })
          });
        </script>
      </body>
    </html>
    

    【讨论】:

    • 听起来不错;我正在考虑创建一个 1px*1px 文本区域,并捕获 CTRL 键和 onMouseDown 元素;当偶数发生时,将处理选定的文本(单选按钮等替换为它们的值),处理后的文本将写入文本区域,选中,焦点将直接指向该区域。这样就有可能实现所需的功能 (?)
    • 幸运的是,它比这更容易。我在原始答案中添加了一个示例。
    • 效果很好,谢谢!复制粘贴到 excel 中时,选择粘贴特殊/文本选项,一切正常。我想我需要在不使用特殊粘贴的情况下完成这项工作。 (目前,复选框也被复制到带有“已检查”字符串的 xls 中)
    【解决方案2】:

    恐怕不可能。但你可以这样做: 有一个指向 javascript 的链接,脚本将隐藏这些项目并以纯文本显示它们的值,因此可以反转此操作。

    这可以使用 JQuery 轻松完成: 这显示了单选值:

    $("input:radio").each(function() {
        if ($(this).attr("checked")) {
            $(this).after('<span class="value">checked</span>');
        }
        else {
            $(this).after('<span class="value">unchecked</span>');
        }
    }).css("display", "none");
    

    我留下了一个跨度类,以便可以使用脚本轻松隐藏这些值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多