带有单选按钮的html表格复制粘贴到xls中

html table with radio button copy paste into xls

本文关键字:xls 复制 表格 单选按钮 html      更新时间:2023-09-26

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

我想要的是,当用户将表的内容复制粘贴到MS Excel中时(纯文本),radio buttons应该用它们的值替换(例如:"已检查"answers"未检查"),列表元素应该用它们选择的值替换,等等。

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

谢谢,krisy

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

有很多问题可以帮助了解实施细节:https://stackoverflow.com/search?q=%5Bjavascript%5D+复制+文本&submit=搜索

更新:事实证明,你甚至不需要删除单选按钮,至少对我的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>

恐怕不可能。但你可以这样做:有一个指向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");

我留下了一个span类,这样就可以使用脚本轻松地隐藏这些值。